Object
Refers to the type System.Object
which is the basis for all other types,
whether reference types or value types.
Such a variable/parameter allows associating any type of data,
where if it is a class,
a simple assignment occurs, and if it is a value type, the Boxing of that value occurs first,
and then an assignment.
In addition, there is special treatment for nullable types
(Nullable<TStruct>
, in C# may be represented by TStruct?
), in which,
if the nullable .HasValue
is false, the value is considered null
, otherwise,
the value contained in nullable is Boxed and then the assignment operation is made.
object x = 1; // vai ocorrer boxing do valor 1, pois Int32 é um value-type
object y = "str"; // não vai haver boxing, pois String é uma reference-type
// nullable é value-type, mas nunca será boxed dentro de um object
int? i = null;
int? j = 10;
object z1 = i; // z1 será null
object z2 = j; // z2 terá um Int32 com o valor 10 boxed e atribuido à variável
var
That’s what you call sugar-syntactic.
It’s just a keyword that’s used for the compiler to guess the type when compiling.
This is called type inference. I’ll be direct:
var x = 1;
var y = "str";
var z = x == 2 ? new MeuObjeto("xpto") : new MeuObject("abc");
will be translated by the compiler to:
Int32 x = 1;
String y = "str";
MeuObjeto z = x == 2 ? new MeuObjeto("xpto") : new MeuObject("abc");
When var
is the only option?
There is a use where var
It’s not just sugary-syntactic.
The big deal in this type inference is anonymous guys, which means,
types that are created inline, and that have no name:
var a = new { Nome = "Miguel" };
There is no C# translation for this. But when compiling,
C# will generate a type instantly, and use it:
<Projection>f__0 a = new <Projection>f__0 { Nome = "Miguel" };
Being generated a class <Projection>f__0
, perfectly valid!
Dynamic
This is a keyword that allows calling methods and properties of any object,
without knowing your type. This is known as late-Binding, that is, is to associate the
call to the destination method/property only at the time the call is made.
Note that when doing Dispatch, a Dispatcher will be used that acts exactly like C#
(or Visual Basic if using Dynamic in VB). This Dispatcher tries to locate the
method/property/operation the same way C# would... and if it fails, a
except that the call cannot be made.
Find out more about Duck-Typing, because the concept is very similar...
but the same.
Performance of dynamic
But don’t think you’ll miss rivers of performance by doing this,
for the implementation of a call on a dynamic
is very efficient.
At the exact point the call is made,
is compiled a call to a static object that caches the method to be called in
a dictionary of types. This means that the call is only slow the first time for each type,
but the second time the same code is executed with the same type, it will be much faster.
You refer to that call as call-site.
Note that a line of code may contain multiple call-sites, one for each operation being done:
// existem 2 call-sites na linha abaixo:
// - uma para a propriedade Length,
// - e outro para o type-cast para int
int a = (int)dyn.Length;
dynamic
is not a guy
dynamic
is not a type, but yes, a keyword that determines a different way of doing
Dispatch (choice of which method/property will actually be called)... to have a parallel,
inheritance and polymorphism are two other ways of doing Dispatch,
different from when dynamic
is used.
Though I’m not a guy, there are types that are associated with the keyword dynamic
.
These guys are part of the DLR (Dynamic Language Runtime)
Reference:
I’ve given a very detailed and exhaustive answer... if you have any further questions, feel free to ask. = D
– Miguel Angelo