I need to know the difference between these three keywords Object
, Dynamic
and var
in C#.
I have seen this link but i don't understand in which case i have to use each one.
Can you explain for me, please, the difference between these keywords ? What are the utilities of each keyword?
dynamic: can change TYPE of the variable, & can change VALUE of the variable later in code. var: can't change TYPE of the variable, but can change the VALUE of the variable later in code.
Dynamic objects expose members such as properties and methods at run time, instead of at compile time. This enables you to create objects to work with structures that do not match a static type or format.
Var is used to declare Implicitly typed variable where the compiler identifies the type automatically. var can hold any type of data. String is an explicit declaration for holding string data only.
Object:
Each object in C# is derived from object type, either directly or indirectly. It is compile time variable and require boxing and unboxing for conversion and it makes it slow. You can change value type to reference type and vice versa.
public void CheckObject() { object test = 10; test = test + 10; // Compile time error test = "hello"; // No error, Boxing happens here }
Var:
It is compile time variable and does not require boxing and unboxing. Since Var is a compile time feature, all type checking is done at compile time only. Once Var has been initialized, you can't change type stored in it.
public void CheckVar() { var test = 10; // after this line test has become of integer type test = test + 10; // No error test = "hello"; // Compile time error as test is an integer type }
Dynamic:
It is run time variable and not require boxing and unboxing. You can assign and value to dynamic and also can change value type stored in same. All errors on dynamic can be discovered at run time only. We can also say that dynamic is a run time object which can hold any type of data.
public void CheckDynamic() { dynamic test = 10; test = test + 10; // No error test = "hello"; // No error, neither compile time nor run time }
Everything is Object
because it is a base type for every type in .net environment. Every type inherit from Object
in a moment, a simple int
variable can be boxed to an object
and unboxed as well. For example:
object a = 10; // int object b = new Customer(); // customer object object c = new Product(); // product object object d = "Jon"; // string object e = new { Name = "Felipe", Age = 20 }; // anonymous type
It is the most abstraction for any type and it is a reference type. If you want to get the real type, you need to unbox
it (using a conversaion strategy such as methods, casts, etc):
object a = "Some Text"; string text = a.ToString(); // call a string method text = text.ToUpper(); object i = 10; // declared as object but instance of int int intValue = (int) i; //declare as an int ... typed
Dynamic
is an implementation of a dynamic aspect in C#, it is not strongly typed. For example:
dynamic a = new Class(); a.Age = 18; a.Name = "Jon"; a.Product = new Product(); string name a.Name; // read a string int age = a.Age; // read an int string productName = a.Product.Name; // read a property a.Product.MoveStock(-1); // call a method from Product property.
var
is just a keyword of the C# language that allows you define any object of a type since you initialize it with a value and it will determinate the type from this value, for example:
var a = 10; // int var b = 10d; // double var c = "text"; // string var d = 10m; // decimal var p = new Product(); // Product type
The compiler will check the type of the value you have defined and set it on the object.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With