Implicit type, object set initialization, anonymous type, initialization Anonymous

Source: Internet
Author: User
Tags reflector

Implicit type, object set initialization, anonymous type, initialization Anonymous

The implicit type and object set initiator are introduced in C #3.0.

1 implicit type

The var keyword is mainly used by the compiler to deduce its type based on the value of the variable.

1.1 implicit local variables

1 class Program2 {3     static void Main(string[] args)4     {5         var stringvariable="learning hard";6         stringvariable=2;7     }      8 }

In fact, when you place the mouse over var, you can still see its type.

There are some limitations when using implicit types, including the following:

(1) The declared variable is a local variable and cannot be a field (including static fields and instance fields ).

(2) variables must be initialized during declaration, because the compiler must deduce the type based on the value assignment of the variables. If they are not initialized, the compiler cannot complete the inference. C # Is a static language. If the variable type is unknown, a compilation error occurs.

(3) A variable cannot be initialized to a method group or an anonymous function.

(4) A variable cannot be initialized to null, because null can be implicitly converted to any reference type or empty type, and the compiler cannot deduce the type of the variable.

(5) You cannot use a declared variable to initialize the implicit type. Run the following code:

string s;var stringvariable=s;

(6) The parameter type in the method cannot be declared using var.

In fact, I personally suggest not to use too much var in the program. If there is a type, specify the type. Such code is more understandable to others.

 

1.2 implicit type array

Using the var keyword, you can not only create implicit local variables, but also create arrays.

1 class Program 2 {3 static void Main (string [] args) 4 {5 var intarray = new [] {1, 2, 3, 4} 6 var stringarray = new [] {"hello", "learning hard "}; 7 // The following example code is incorrect: 8 var errorarray = new [] {"hello", 3}; 9} 10}

When using an implicit array, the compiler must be able to deduce the array type. The compiler first constructs a set of compile-time types that contain all the expressions in braces. In this set, if all types can be implicitly converted to a unique type, the type will become the array type; otherwise, a compilation error will occur.

 

2. object set Initiator

2.1 object initializer

Do not speak about anything. first go to the Code:

 1 class Program 2     { 3         static void Main(string[] args) 4         { 5             Person p = new Person() {Name = "Hong", Age = 25, Weight = 65, Height = 170}; 6         } 7     } 8  9     public class Person10     {11         public string Name { get; set; }12         public int Age { get; set; }13         public int Weight { get; set; }14         public int Height { get; set; }15     }

In fact, this is the help of the compiler. Let's take a look at it with Reflector:

Note: To use the object initialization tool, you must ensure that the class has a non-argument constructor. If you customize a constructor with parameters and overwrite the default no-argument constructor, You need to redefine a no-argument constructor.

2.2 set Initiator

In fact, the principle is the same as the above object initializer. You can use Reflector to decompile and view it.

1 class Program2 {3     static void Main(string[] args)4     {5         List<string> newnames=new List<string>{"111","2222","4433"};6     }7 }

3. Anonymous type

1 class Program 2 {3 static void Main (string [] args) 4 {5 var person = new {Name = "Hong", Age = 12}; 6 Console. writeLine ($ "{person. the age of Name} is {person. age} "); 7 8 var persons = new [] 9 {10 new {Name =" 1 ", Age = 2}, 11 new {Name =" 2 ", age = 3}, 12 new {Name = "3", Age = 4} 13 14 // if you add the following line, the compilation error is 15 // new {Name = "6"} 16}; 17 int totalAge = persons. sum (per => per. age); 18 Console. writeLine ($ "Total Age of all people is {totalAge}"); 19 Console. readKey (); 20} 21}

Result:

According to the running results and anonymous code, the Code defined by the class can be omitted using the anonymous type. Use Reflector to view the decompilation results:

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.