From: http://social.msdn.microsoft.com/Forums/de-DE/visualcshartzhchs/thread/4b55fc5b-fa87-4861-b1e3-3be397612872
Implicit type
From Visual C #3.0, the implicit variable type VaR can be declared in the method body. We can use the modifier VaR as shown below
Tell the compiler to deduce and declare the type:
VaR I = 23; // int I = 23;
VaR S = "hello"; // string s
= "Hello ";
Arrays can also be declared as implicit types:
VaR A = new [] {1, 2, 3, 4}; // int []
VaR B
= New [] {"hello", null, "world"}; // string []
VaR c = new [] {A, new [] {5,
6, 7, 8 }}; // an array with an uncertain length
Note:
Variables that declare implicit types have the following restrictions:
• VaR can only be used for local variable declaration and must be instantly initialized. This variable cannot be initialized to null because null is of no type like lambda expressions or methods. However, it can be initialized to an expression that gets a null value, as long as the expression has a type.
• Var cannot be used as a class field
• Variables declared by the VaR type cannot be used as self-initialized expressions.
In other words, VAR
V = V ++; an error will be reported during compilation.
• Composite implicit types cannot be initialized at the same time
• If we define a var variable in the scope and try to use the VaR keyword to initialize a local variable, an error will be reported during compilation.
For more information, see implicitly.
Typed local variables (C # Programming Guide) and type Relationship in LINQ Query
Operations (C #).
anonymous type
anonymous type provides a convenient method to encapsulate a set of read-only attributes into a single object without explicitly defining a type.
the type name is generated by the compiler and cannot be used at the Source Code level. The types of these attributes are inferred by the compiler. The following example shows an anonymous type that is initialized with two attributes: Amount and message
.
var v = new {amount = 123, message = "hello"
};
An anonymous type is a class consisting of one or more public read-only attributes.
other types of class members (such as methods or events) are not allowed ).
Note:
anonymous rules:
• except for the second point, the name must be provided for the attribute initialized using the expression
• If the member name is not specified in the anonymous type, the compiler will specify the same name as the attribute used to initialize the anonymous type members
• the anonymous type has a method range
other details, see anonymous
types (C # programming guide)