开发者

Can dynamic variables treated as "int" overflow?

Suppose you have a dynamic variable being treated as an int (value is less the int.MaxValue).

At some point in your application the dynami开发者_开发问答c value increases and surpasses the "int" value.

Will the application crash or treat it (convert) as a long?


Suppose you have a dynamic variable being treated as an int (value is less the int.MaxValue).

By "being treated as int" I assume you mean "containing a value of runtime type int".

At some point in your application the dynamic value increases and surpasses the "int" value.

OK. How? You omit the most important part of the question. How did the value increase?

Will the application crash or treat it (convert) as a long?

Sometimes it will crash, sometimes the result will be long, sometimes the result will be double, or decimal, sometimes the int will wrap around. Since you haven't said how it is that the value is being made to increase it is impossible to answer your question.

In general, the rule for dynamic is that the dynamic code will behave at runtime as the equivalent non-dynamic code would have behaved if the compile-time type had been known. If the compiler would have given an error, then the runtime gives an error. If the compiler would have added two ints to produce a third, then the runtime will add two ints to produce a third. If the compiler would have added an int and a double to produce a double, then the runtime will add an int and a double to produce a double. And so on.


Easy to test in LINQPad:

void Main()
{
    dynamic i = int.MaxValue - 10;
    i += 15;
    Console.WriteLine(i.GetType());
    Console.WriteLine(i);
}

Output:

typeof (Int32) 
-2147483644


depends of the Type of the inc value.

    static void Main(string[] args)
    {
        dynamic i = int.MaxValue;
        long l = 15;
        i += l;

        Console.WriteLine(i.GetType());
        Console.WriteLine(i);

        Console.ReadLine();

    }

in my case the datatype is long and not int anymore like in the sample of Lasse. (dangerous game)

output:

System.Int64
2147483662


At some point in your application the dynamic value increases and surpasses the "int" value.

A int value is immutable, it cannot be increased. If another value is being assigned to a variable of type dynamic then the runtime type of that variable will be whatever the type of the assignment is.

If you are adding two ints, then the result will be another int. If you are adding an int and a long, then the result will be a long. This is because of the return type of the operator+ method that is chosen by the runtime compiler.

Also, if you are adding two ints in an unchecked context, then an overflow will not crash the program.


I have tested it with this code:

        dynamic a = 0;
        a = Int32.MaxValue;

        // a is now = Int32.MaxValue

        a += Int32.MaxValue;

        // a is now = -2

It does not crash. GetType of a reports that a is Int32.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜