A value will automatically be cast to the appropriate type if the compiler knows that it can always be converted to that type.
int number = -1;
object value = number;
Console.WriteLine(value);
In this example, we didn't need to use the typical explicit casting syntax because the compiler knows all int
s can be cast to object
s. In fact, we could avoid creating variables and pass -1
directly as the argument of Console.WriteLine()
that expects an object
.
Console.WriteLine(-1);