Is it same or any difference between Implicitly Typed and Anonymous Type.If it is different so What is the main difference between Implicitly Typed and Anonymous Type?

有帮助吗?

解决方案

There is a huge difference:

Implicitly typed (local) variables, are variables which type is not explicitly given:

var i = new StringBuilder();

Now, i is implicitly of type StringBuilder - a named type.

Anonymous types on the other side do not have a name, they are anonymous:

var x = new { Foo = "Bar" };

x is now of an anonymous type, with a read-only property Foo.

其他提示

Anonymous type can be used only in the scope where it was declared. So you can use it to local tasks and can't use it in the outer scope.

The anonymous types was introduced to use with linq to achive an easy grouping and transformations. The main goal is avoid an explicit type declaration which will used in the single scope.

List<TypeOne> firstList;
List<TypeTwo> secondList;

var joined = firstList
    .GroupJoin(secondlist, i => i.KeyOfTypeOne, i => i.KeyOfTypeTwo, (first, second) => new { First = first, Second = second});

So a new anonymous type is declared and you can use it in the same context but can't send to the boundaries of the current scope as a strongly typed object

TypeName Method() // you can't specify a type here except object or dynamic
{
   ...
   return joined;
}

but you can use it locally without any restrictions

return joined.Where(i => i.Second.Any()).Select(i => i.First);

And the implicitly typed variables on the other hand is used to allow you to use object of an anonymous type. Compilator will investigate your code and put real type instead of the var keyword.

var inst = new {Name = "a";}

compilator will generate an anonymous type "{Name = "a";}" and use it to replace var in your code.

There are some difference between them

Implicitly Type

We can know which type of datatype or class assign to var

e.g.

var i = 10; /*here int datatype assigned*/
var A = new ArrayList(); /*here arraylist() class assigned*/

we can use implicitly typed in another scope also.

Anonymous Type

when we are not sure about which type of class assigned to var

e.g.
var a = new { Age = 12 , Name = X };

Anonymous variable is used only in the scope where it was declared. but can't use in another scope and can't write in return statement of method.

Generally Anonymous used in Linq statement.

From Implicitly Typed Local Variables (C# Programming Guide):

Local variables can be given an inferred "type" of var instead of an explicit type. The var keyword instructs the compiler to infer the type of the variable from the expression on the right side of the initialization statement. The inferred type may be a built-in type, an anonymous type, a user-defined type, or a type defined in the .NET Framework class library.

Also:

It is important to understand that the var keyword does not mean "variant" and does not indicate that the variable is loosely typed, or late-bound. It just means that the compiler determines and assigns the most appropriate type.

There is a key relation between anonymous types and implicitly typed variables:

In many cases the use of var is optional and is just a syntactic convenience. However, when a variable is initialized with an anonymous type you must declare the variable as var if you need to access the properties of the object at a later point.

There are few fundamental differences between these

Anonymous Types:

They are effectively class types with nothing other then read-only properties. No Methods no events. They cannot be accessed/declared outside the local scope. E.g if defined inside a method they cannot be accessed outside that method.

Implicitly Typed

We can use var as an implicit type. This effectively means that the type of whatever is on the right side of expression will be assigned to this var by the compiler. These cannot be null.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top