Use var keyword to let the compiler figure out the type of a variable from its "initializer". - CSharp Language Basics

CSharp examples for Language Basics:var

Introduction

That's what "implicit type inference" means: "Let the compiler figure it out."

Demo Code

using System;/*from  w  w w.jav a  2s  .  c  o  m*/
class Program
{
   static void Main(string[] args)
   {
      var i = 5;        // Same as int i = 5;
      Console.Write(i.ToString() + " is a ");
      Console.WriteLine(i.GetType().ToString() + "\n");
      var s = "Hello";  // Same as string s = "Hello";
      Console.Write(s + " is a ");
      Console.WriteLine(s.GetType().ToString() + "\n");
      var d = 1.0;      // Same as double d = 1.0;
      Console.Write("{0:f1} is a ", d);
      Console.WriteLine(d.GetType().ToString());
   }
}

Result


Related Tutorials