Anonymous and Named types ASP.Net

Ratings:
(4)
Views:0
Banner-Img
  • Share this blog:

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 framework and C#.

2Anonymous types cannot be used as Global variables.

2Initialize the data into an anonymous type is mandatory.

Inclined to build a profession as ASP.net Developer? Then here is the blog post on, explore ASP.net Training

Different between Anonymous and Named types

 

Named Type Anonymous
  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 an anonymous type
  1. Can be used as a local /global variable
Can be used as a local variable 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 the 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 the 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 variables can’t be declared in the same statement as var a=10,b= “sai” raises compilation error
  1. Can be used with unsafe code
Can’t be used with unsafe 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();
}
}
}

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();
}
}
}

You liked the article?

Like : 0

Vote for difficulty

Current difficulty (Avg): Medium

Recommended Courses

1/15

About Author
Authorlogo
Name
TekSlate
Author Bio

TekSlate is the best online training provider in delivering world-class IT skills to individuals and corporates from all parts of the globe. We are proven experts in accumulating every need of an IT skills upgrade aspirant and have delivered excellent services. We aim to bring you all the essentials to learn and master new technologies in the market with our articles, blogs, and videos. Build your career success with us, enhancing most in-demand skills in the market.


Stay Updated


Get stories of change makers and innovators from the startup ecosystem in your inbox