Use var keyword with arrays and collections. - CSharp Language Basics

CSharp examples for Language Basics:var

Description

Use var keyword with arrays and collections.

Demo Code

using System;//from w  ww.ja va  2s  . c om
using System.Collections.Generic;
class Program
{
   static void Main(string[] args)
   {
      // Use var for a generic list type.
      // Note the use of the new collection initializer syntax, too.
      var names = new List<string> { "C", "B", "S", "M" };
      Console.WriteLine("List of names: ");
      foreach (string s in names)
      {
         Console.WriteLine(s);
      }
      Console.WriteLine(names.GetType().ToString());
   }
}

Result


Related Tutorials