Tuesday, April 14, 2009

ANONYMOUS TYPES

VAR keyword specifies a special anonymous type which can specifies all the types likeinteger,float,strings and even classes.Anonymous Types are the syntactic sugar basically.This Can also be done With Methods Then we Call it anonymous methods.

using System;
using System.Collections.Generic;
using System.Text;

namespace ANONYMOUSTYPES
{
  class Program
  {
  static void Main(string[] args)
  {
   
  // due to var keyword Compiler itself make classes of following objects.
   
  var student1 = new { name = "sumair", fathername = "irshad", age = 20 };
  var student2 = new { name = "andrew", fathername = "Hejisberg", age = 20 };
  var teacher = new { name = "BillGates", company = "Microsoft" };
  //now compiler will define myint as integer.
  var myint =4;
  //now compiler will define mystring as string.
  var mystring = "csharp";
  Console.WriteLine("STUDENTS:\n NAME = "+student1.name+" FATHERNAME = "+student1.fathername+"AGE ="+student1.age);
  Console.WriteLine(" NAME = "+student2.name+" FATHERNAME = "+student2.fathername+"AGE ="+student2.age);
  Console.WriteLine("\nTEACHER:\n NAME = " + teacher.name + "\n COMPANY = " + teacher.company);
  Console.WriteLine("\nSTRING = {0} \nINTEGER = {1}", mystring,myint);
  Console.ReadKey();
  }
  }
}
OutPut:

STUDENTS:

 NAME = sumair  FatherName = irshadAge= 20
 NAME = andrew FatherName = HejisbergAge= 20

TEACHER:

  NAME = BillGates

  Company = Microsoft

 STRING = csharp

INTEGER = 4

 

No comments:

Post a Comment