. NET framework3.5 new features 2:var, initialization, anonymous classes, and extension methods

Source: Internet
Author: User
Tags anonymous constructor

Related article:. NET framework3.5 new feature 1:LAMBDA expression

Define variables with Var

In c#3.0, a new way of declaring variables is provided, which is var. With this keyword, you do not need to specify the type when declaring a variable, which is determined by the compiler at initialization time. The code is as follows:

var ss="abcd";
MessageBox.Show(ss.GetType().ToString());

The code above will show System.String, proving that the C # compiler has compiled SS into a String variable. And after the output SS, then enter the "." , you will see that the corresponding methods and properties of the string type variable are also listed, so you can conclude that C # considers SS as string rather than object. So using var to define variables can also have the advantages of object and strong types.

But don't look at Var as JavaScript's Var, the difference being that JavaScript is a weakly typed language, and the variables in JavaScript (also including variables declared with Var) can transform types, as shown in the following javascript:

var s="abcd";
s=3;
alert(s);

The code above assigns a string to S for the first time, and the second line of code assigns an integer. Such code has no problem with JavaScript. But in c#3.0, the Var variable is initialized and the type cannot be changed after the type is determined. such as the following code cannot be compiled through:

var ss="abcd";
ss=44;

To sum up, the following four features are used to define variables using var:

1. Must be initialized at the time of definition. That is, it must be var s = "ABCD" form, not the following form:

var s;
s=“abcd”;

2. Once the initialization is complete, the variable can no longer be assigned a different value than the initialization value type.

3.var requirements are local variables.

4. By using the var definition variable and object, it is exactly the same in terms of efficiency and the use of a strongly typed method to define variables. But I suggest that if you know the type of the variable in advance, use a strongly typed method to declare the variable. Otherwise, it can make it difficult for developers to determine what type of variable is because of the high use of Var. This is not conducive to program maintenance and upgrades.

Although Var has pros and cons, I personally believe that if you convert a dynamic language into a C # language, you might consider using Var to define variables. This is because a dynamic language has no type, and to convert it to a strongly typed C # language, you must specify a type for the variable, but it is laborious to determine the type beforehand, rather than assigning it as Var and then the C # compiler to determine the specific type of the variable. What if, in the process of conversion, you find that the variables of the dynamic language change the type? This problem can be solved by using the "anonymous class" in part three.

Second, initialization

If a class has public fields, you can use the following code to initialize the object instances of the class when you create them;

public class MyClass
{
Public String field1;
public int field2;
public bool field3;
}

MyClass my = new MyClass ();
My.field1 = "ABCD";
My.field2 = 44;
My.field3 = true;

A more convenient way to initialize these public variables is provided in c#3.0, and the code is as follows:

MyClass my=new MyClass
{
  field1=“abcd”,
   field2=44;
  field3=true;
};

The code above is somewhat like a constructor with a parameter, but this will not invoke the MyClass constructor (because MyClass does not have a constructor with three arguments), but rather a magic trick that the C # compiler plays. In fact, the code above is compiled and still the same way as using traditional initialization fields. It just looks simpler in syntax (at least not so many my). To be aware, initialization with this method must be a field of public (a field that cannot be a protected, private, or default modifier).

The initialization of the collection class (similar to an array in the way it is initialized) is also improved in c#3.0. Unfortunately, this initialization only supports a generic collection class, that is, only the collection class that implements system.collections.generic.icollection<t> can use this initialization method. The code is as follows:

List<string>myList=new List<string> {"data1","data2","data3"};
foreach (string data in myList)
{
  textBox1.AppendText(data);
}

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.