Anonymous and Named types ASP.Net

Different between Anonymous and Named types

Anonymous

2In general anonymous types of declared with var keyword like

var a =10;

var b=20.5;

var c=”sai”;

2What we use the data type in C# like int, string, double etc. are known as “named types”.

2Anonymous types are introduced in 2.0 versions of .net frame work and C#.

2Anonymous types cannot be used as Global variables

2Initialize the data into anonymous type is mandatory.

At TekSlate, we offer resources that help you in learning various IT courses.

We avail both written material and demo video tutorials.

To gain in-depth knowledge and be on par with practical experience, then explore ASP.net Training Videos.

Different between Anonymous and Named types

 

Named TypeAnonymous

  1. Type is known at source time

Type is known at compile time

  1. User/ programmer will decide the type of the variable

Compiler will decide the type of the variable

  1. Use respective type keyword to create a variable with names type

Use var keyword to create a variable with anonymous type

  1. Can be used as a local /global variable

Can be used as a local variables only and can’t be used as a global variable data field of the class

  1. Can be used in any context

Can be used in following situations

  • As local variable
  • In for loop initialization
  • In for each loop initialization
  • In using statement

  1. Initialization of variable is optional

Initialization of variable is mandatory

  1. In case of number types can be used with increment/decrement unary operator

In case of number types cannot be used with increment/ decrement unary operator  -> for a = a++ produces compile time error

  1. Multiple variables can be declared in same statement like object a=10,b= “sai” , c= 10.5

Multiple variable can’t be declared in same statement like var a=10,b= “sai” raises compilation error

  1. Can be used with un safe code

Can’t be used with un safe code

  1. Doesn’t support complex types

Support complex types

Examples with Anonymous types:

Name space CA sample

{

Class sample

{

Static void main ()

{

Var a=”10”;

Var b=”sai”;

Var c=”10.5”;

c.w.l(“value of a :-“+a);

c.w.l(“value of b :-“+b);

c.w.l(“value of c :-“+c);

console.read();

}

}

}

  1. Create an array and print elements using Anonymous type?

Name space CA sample

{

Class sample

{

Static void main ()

{

Int []A=new int [6]{10,20,30,40,50,60};

String []B=new string[3]{“sai”,”Ram”,”krishna”};

c.w.l(“elements of array A are:-”);

for each (var X in A)

c.write (X+” “);

c.wl (“/n element of array B are:-“);

for each (var X in B)

console write (X+” “);

console read ();

}

}

}

Name space CA sample

{

Class example3

{

Static void main ()

{

Var Pname =New {Sname=”Sachin”, Fname=”Ramesh”, Lname=”tendulker”};

Var DOB=new {Day=14, month=”April”, year=1973};

c.w.l(“person name is:-”+Pname.fname+Pname.Lname);

c.w.l(“DOB is:-”+DOB.month+”-”+DOB.year);

c.Read();

}

}

}

For indepth understanding click on