top of page

"Var" Keyword in C#

Var is a keyword, it is used to declare an implicit type variable, that specifies the type of a variable based on initial value.


The var keyword can be used in the for loop, foreach loop, using statements, anonymous types, LINQ, and other places.


Syntax:

var variable_name = value;

When var is used with nullable reference types enabled, it always implies a nullable reference type even if the expression type isn't nullable. The compiler's null state analysis protects against dereferencing a potential null value. If the variable is never assigned to an expression that maybe null, the compiler won't emit any warnings. If you assign the variable to an expression that might be null, you must test that it isn't null before dereferencing it to avoid any warnings.


Following are the important points which we need to remember about var keyword in c#.

  • var keyword is useful to declare the implicitly-typed local variables without specifying an explicit type.

  • In c#, the type of implicitly-typed local variables will automatically determine by the compiler based on the right-side value of the initialization statement.

  • The var variables must be declared and initialized in the same statement.

  • We are not allowed to assign a null value to the implicitly-typed local variables.

  • Cannot initialize the multiple implicitly-typed variables in the same statement.

  • var is not allowed to use as a field type at the class level.


Code:

// C# program for var keyword
using System;
using System.Text;
  
namespace Test {
  
class abs {
  
    static void Main(string[] args)
    {
  
        var a = 165.23f;
        var b = 290.23m;
        var c = 'T';
        var d = "Hello World!";
  
        // to print their type of variables
        Console.WriteLine("value of a {0}, type {1}", a, a.GetType());
        Console.WriteLine("value of b {0}, type {1}", b, b.GetType());
        Console.WriteLine("value of c {0}, type {1}", c, c.GetType());
        Console.WriteLine("value of d {0}, type {1}", d, d.GetType());
  
        // hit ENTER to exit
        Console.ReadLine();
    }
}
}

Output:









Benefits of using var keyword in C#

The following are the advantages of using the var keywords in C#.

  • var is used to hold the result of a method whose type is not known such as an anonymous method, LINQ expressions, or generic types.

  • var is type-safe, the value assigned to the var variable is known by the compiler at the compile time which prevents any issue at the runtime.

  • var does not require boxing and unboxing.

  • Improve focus on readability of code, It is a shorthand way of declaring a var when the class or struct names are very long.

  • Visual Studio shows the intelligence because the type of variable assigned is known to the compiler at the compile time.


Limitation of using var variable in C#

The following are the limitations of using the var variable in C#.

  • var variable must initialize in the same statement

  • While creating a var variable, we must make sure that the variable is declared and initialized in the same statement to avoid the compile-time error.

  • The var variable cannot be initialized with a null value.


The Tech Platform

0 comments

Comments


bottom of page