11+ Year IT Industry Experience, Working as Technical Lead with Capgemini | Consultant | Leadership and Corporate Trainer | Motivational and Technical Speaker | Career Coach | Author | MVP | Founder Of RVS Group | Trained more than 4000+ IT professionals | Azure | DevOps | ASP.NET | C# | MVC | WEB API | ANGULAR | TYPESCRIPT | MEAN | SQL | SSRS | WEB SERVICE | WCF... https://bikeshsrivastava.blogspot.in/ http://bikeshsrivastava.com/

What is difference between constant,readonly,dynamic,var and static in C# ?

Today i am going to explain about constant(const),readonly,dynamic.var and static in c#.Also i going to differentiate about these c# keyword because most of developer confused about these keyword in c#. 




                        constant (const)
In c#  the constants refer to fixed values of variable  that the program may not change during its execution time . These fixed values of variable  are also called literals.you can assign constants can be of any of the basic data types like an integer, a floating, a character,or a string literal. See image ..........




                             readonly
In the c# readonly keyword is use as  a modifier for field. Whenever you a field declaration includes a readonly modifier in c#, assignments to the fields value  introduced by the declaration can only occur as part of the declaration or in a constructor in the same class.A field that can  marked as "readonly", can only be set once during the construction of an object (in the constructor). See image..........



"Readonly" is the keyword whose assigned  value we can change during runtime process or we can assign value  it at run time but only through the non-static constructor. Not even a method or function.

                                    static
In c# each object has its own set of member,properties ,variables and all the member variables have a scope part.
If you want a variable to have the same value throughout all instances of the object then we can declare it as a static variable in our program. To manipulate and use the values of static variables we can also define a function as static.

In c# the keyword "static" means that only one instance  of a given variable exists for a class. Static variables are used to define constants because their values can be retrieved by invoking the class without creating an instance of it.


In c# static variables can be initialized outside the member function or class definition. The following code is an example of a static variable, which shows the declaration and initialization of a static variable.See image.......


                                                               Dynamic
In c# 4.0 the dynamic keyword is new, and is used to tell the compiler that a variable's type can change or that it is not known until run-time.It means, their type is inferred at run-time and not at  the compile time execution  in contrast to var type.See image............




                                           var
From c# 3.0 “Var” Keyword = Var keyword is an implicit way to defining Datatypes. Implicit means indirect way to defining variable types.

In  c# it means data at the right hands side the left hands side data types is defined by the compiler during the generation of the “IL” code. See image..........




              
       Difference between "object" "var" and "dynamic"


Object keyword
Var keyword
Dynamic keyword
Added in C# 1.0.
Added in C# 3.0.
Added in  C# 4.0.
object in c# can store any kind of value,data, it is base class in c#.
It can also store any type of value but It must to initialize var types value at the time of declaration .
It can also  store any type of the variable, 
its Compiler has little information about the type.store in heap.
It is type safe i.e. Compiler has all information about the stored value, so that it doesn't cause any issue at run-time.
It is not type safe i.e. Compiler doesn't have any information about the type of variable.
it  can be passed as method argument and method also can return object type data .
Var type cannot be passed as method argument and method cannot return object data. Var type work in the scope where it defined.
Dynamic type can be passed as method argument and method also can return dynamic type.
Need to cast object variable to original type to use it and performing desired operations.
No need to typecast because compiler has all information to perform operations.
Casting is not required but you need to know the properties and methods related to stored type name .
Cause the problem at run time if the stored value is not getting typecast to underlying data type.
Doesn't cause problem because compiler has all information about stored value.
Cause problem if the wrong properties or methods are accessed because all the information about stored value is get resolve only at run time.
Useful when we don’t have more information about the data type.

=>Intelligence available  
Useful when we don’t know actual type i.e. type is anonymous.


=>Intelligence available 
Useful when we need to code using reflection or dynamic languages or with the COM objects, because you need to write less code.
=>Intelligence not  available 

   
Difference between "readonly" "const" and "static"




You have just read an article that categorized by title C# / Interview Question by title What is difference between constant,readonly,dynamic,var and static in C# ? . You can bookmark this page with a URL https://bikeshsrivastava.blogspot.com/2016/07/part-31what-is-difference-between.html. Thank You!
Author: Bikesh Srivastava - Monday, July 25, 2016

4 comments to "What is difference between constant,readonly,dynamic,var and static in C# ? "

  1. In c# the constants refer to fixed values of variable that the program may not change during its execution time . These fixed values of variable are also called literals.. dot net training in velachery | dot net training in chennai

    ReplyDelete


  2. Given so much information in it. its very useful .perfect explanation about Dot net framework.Thanks for your valuable information. dot net training in velachery | dot net training in chennai

    ReplyDelete
  3. I like your post very much. It is very much useful for my research. I hope you to share more info about this. Keep posting angular certification

    ReplyDelete

Life Is Complicated, But Now programmer Can Keep It Simple.