Implicitly Typed Local Variables in C#

We usually specify the data type of the variable while declaring it like int age; string name; etc. This is known as explicitly typed variables.

But sometimes, we need to create a variable that can hold any variable of any data type. We can do this using var keyword. This is known as implicitly typed variables. The var keyword can be used in place of specifying a specific data type such as int, string etc. When you do so, the compiler will automatically infer the underlying data type based on the initial value used to initialize the local data point.

Below is a sample code:

var myInt = 1234;
var myBool = true;
var myStr = "Hello";

Console.WriteLine("Value of myInt is: " + myInt + " and type of myInt is: " + myInt.GetType().Name);
Console.WriteLine("Value of myBool is: " + myBool + " and type of myBool is: " + myBool.GetType().Name);
Console.WriteLine("Value of myStr is: " + myStr + " and type of myStr is: " + myStr.GetType().Name);

Below is the output of the above code:

Restrictions on implicit typed local variables

    • Implicit typing applies only to local variables in a method or property scope.
    • It is illegal to use var keyword to define return values, parameters or field values of a custom type.
      For example, below are invalid usage of var keyword:
      class varUsage
      {
      // Error: var cannot be used as field data
      private var age=100;
      // Error: var cannot be used as a return value or parameter type
      public var Add(var a, var b){}
      }
    • Local variables declared with the var keyword must be assigned an initial value at the time of their declaration. We cannot assign null value to varExample:// Error: Must assign an initial value to var variables
      var data;// Error: Must assign value at the time of declaration
      var data;
      data = “Hello”;// Error: Can’t assign null
      var data = null;

      // No error as this is a reference type;
      var car = new Vehicle();
      car = null;

      // Below code is also fine
      var data = “hello”;
      var data1 = data;

    • It is allowed to return an implicitly typed local variable to the caller, provided the method return type is the same as of var-defined data type.Example:
      static int GetValue()
      {
      var value=9;
      return value;
      }

    Implicit typed data is strongly typed data

    Implicit typed data effects only at compile time. After that, that variable is treated as if it was declared with that data type. You cannot assign a value of different data type to that variable, even at later point of time. It will result in compile time error.
    Example:
    static void GetValue()
    {
    var str = “Hello World”;
    str = “Hiiii”;

    // Error: Cannot implicitly convert type ’int’ to ‘string’
    str = 1234;
    }

Power Platform Academy

Start or Upgrade your Career with Power Platform

Learn with Akanksha

Python | Azure | AI/ML | OpenAI | MLOps

Design a site like this with WordPress.com
Get started