Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between Object, Dynamic and Var

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?

like image 627
Lamloumi Afif Avatar asked Jan 12 '14 21:01

Lamloumi Afif


People also ask

What is difference between VAR and dynamic in flutter?

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.

What is a dynamic object?

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.

What is the difference between VAR and string?

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.


2 Answers

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 } 
like image 153
Love Gupta Avatar answered Oct 06 '22 20:10

Love Gupta


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.

like image 37
Felipe Oriani Avatar answered Oct 06 '22 19:10

Felipe Oriani