C # 3.0 Features

Source: Internet
Author: User
Tags anonymous

In this article I want to introduce two concepts, I think these two things must be introduced together, so as to be consistent.

C # 2.0 We have been anonymous methods, and now the type also play anonymous come, no wonder everyone "report" when all like anonymity, why? Because the anonymous is the informant can not find revenge object Bai, yes, anonymity is to hide the name, no name who can find you ah.

Anonymous type

There are some types in C # that are stored as temporary data, the lifecycle is only within this method, the method ends, and the life cycle of this type is gone. So here we can use an anonymous type.

var KeyPair = new {key= "Yuyi", value= "20"};

This keypair is an anonymous type, note that keypair here is a variable name, not the name of the class. Well, there's a var in front, what is this? This is an implicit local variable in C # 3.0.

Implicitly typed Local variables

Let's introduce the implicit type local variables first:

In C # 3.0, one more keyword, VAR, says one type: The C # compiler can infer it from context such as var I = 5; The compiler can infer from the following assignment that I should be an integral type. Since it is a local variable, it can only be used inside the method, noting that C # is strongly typed and introducing a VAR that is not a weakly typed language like JavaScript. var is substituted for the type determined by the compiler the first time it is compiled. Therefore, the following points should be noted for implicitly typed local variables:

1. It can only exist within the method

2. It is not a new type, just a keyword, or is called a placeholder, which is replaced by the defined type after the C # compiler compiles

3. It is inferred from the context of the compiler, so it is wrong to use everything that cannot be inferred by the compiler. For example, you can't use this: var nullvalue = null; Because null is nothing, he is an empty pointer, is an indeterminate thing. Nor can it be used in this way: var I = 5;i = "abc"; the compiler infers that it is an integer based on the first assignment, but then assigns a string to it, what's going on?

For Var my advice is not to force the time without, then what is forced to do? Let's look at our anonymous type.

Back to Anonymous type

Just said, anonymous type is no name type, no name you how to call it, how to declare it? But does the anonymous type really have a name?

Look at what the C # compiler has done behind us:

Using ILDASM to open a compiled assembly, you find one more type:

<>f__AnonymousType0<<Key>j__TPar,<Value> j__TPar>

This type is directly inherited from System.Object and is internal Seald (visible only within the assembly and cannot be inherited). You may find that this type is still a generic type, so as long as we use an anonymous type with the number of parameters, the name of the parameter does not change, the compiler will not generate more types for us:

var KeyPair1 = new {key= "Yuyi", value= "Programer"};
var KeyPair2 = new {key= "Y", value=3};
var KeyPair3 = new {key=4,value= "abc"};

The above three anonymous types, the compiler will only generate a new type for us behind, a generic type. If we modify the name of an attribute within this anonymous type: the

var KeyPair1 = new {key= "Yuyi", value= "Programer"};
var KeyPair2 = new {key= "Y", value1=3};

Two new generics are generated:

<>f__AnonymousType0<<Key>j__TPar,<Value> j__TPar>
<>f__AnonymousType1<<Key>j__TPar,<Value1>j__TPar& gt;

Look, the name is still a rule to follow oh. What if you add a new attribute to this anonymous type? This creates a new type:

<>f__AnonymousType1<<Key>j__TPar,<Value1> ;j__TPar,<Test>j__TPar>

Well, this issue is still a concern, so we should try to keep "consistent" when using anonymous types:

The number of attributes is consistent (this is as far as possible).

Property name is consistent, this is better than good grasp.

As long as this consistency is maintained, the compiler will produce the same type for consistency, and inconsistent will generate a new type, if not too much, I would like to be a "code explosion" and cause "workset" too large loss of performance? This is only my personal opinion, has not been tested.

Continuation of implicitly typed local variables

Because anonymous types do not exist when we write code, anonymous types cannot be the return value and parameters of the method. Like "Var", it can only be used within a method. Is it a little clear when you're forced to use "var"? Anonymous type compilers can be inferred when using anonymous types, but they cannot be inferred manually. So I think that implicitly typed local variables are used only when the compiler can infer and people cannot infer. It is a good programming practice to make the code more readable by explicitly declaring variable types, which we can infer or not recommend, and not because C # 3.0 provides such features on a large and special use.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.