What does the keyword var do in C#?

CsharpProgrammingServer Side Programming

The "var" keyword initializes variables with var support. Just assign whatever value you want for the variable, integer, string, float, etc.

Example

 Live Demo

using System;

namespace Demo {
   class Program {
      static void Main(string[] args) {
         var myInt = 5;
         var myString = "Amit";
         Console.WriteLine("Rank: {0} \nName: {1}",myInt,myString);
      }
   }
}

Output

Rank: 5
Name: Amit

We can also use var in arrays −

Example

 Live Demo

using System;

namespace Demo {
   class Program {
      static void Main(string[] args) {
         var myInt = new int[] {65,43,88,56};
         foreach(var val in myInt)
         Console.WriteLine(val);
      }
   }
}

Output

65
43
88
56
raja
Published on 09-Aug-2018 10:03:30
Advertisements