Monday, January 15, 2024

Var Vs Dynamic Keywords In C#

If you're working with C# code, I assume you're utilizing the 'var' and 'dynamic' keywords. Here are some highlighted points that I hope you find helpful.

Var Vs Dynamic Keywords In C#

When were they introduced

  • var was introduced in C# 3.0
  • dynamic was introduced in C# 4.0.

Type inference of variables

  • var is a statically typed variable. It results in a strongly typed variable, in other words the data type of these variables are inferred at compile time. This is done based on the type of value that these variables are initialized with.
  • dynamic are dynamically typed variables. This means, their type is inferred at run-time and not the compile time in contrast to var type.

Initialization of variables

  • var type of variables are required to be initialized at the time of declaration or else they encounter the compile time error: Implicitly-typed local variables must be initialized.
  • dynamic type variables need not be initialized when declared.

If you want to understand kindly refer this link click here  


Thank you ,

No comments:

Post a Comment