C # var declaration variable resolution

Source: Internet
Author: User

C # VAR declares variable resolution:
A new way of declaring variables is provided in c#3.0, which is var.

With this keyword, you do not need to specify a type when declaring a variable, and the variable type is determined by the compiler at initialization time. The code is as follows:
var ss = "ABCD";
MessageBox.Show (ss. GetType (). ToString ());

The above code will show System.String, proving that the C # compiler has compiled the SS into a String variable.

After the output SS, enter "." , you see that the corresponding methods and properties of the string type variable are also listed, so you can conclude that C # sees SS as a string type, not an object.

So using var to define variables can also have the advantage of object and strong typing.

However, you should not consider Var as the JavaScript Var, the difference is that JavaScript is a weak type of language, and the variables in JavaScript (also including variables declared with Var) can transform the type,

As shown in the following javascript:
var s = "ABCD";
s=3;
alert (s);

The above code assigns a string to S for the first time, and the second line of code assigns an integer. Such code does not have any problems in JavaScript.

However, in c#3.0,when the Var variable is initialized and the type is determined, the type cannot be changed. The following code is not compiled by:
var ss = "ABCD";
SS = 44;

In summary, the following four characteristics are used when defining variables using var:
1. Must be initialized at the time of definition. It must be var s = "ABCD" form, not the following form:
var s;
s = "ABCD";
2. Once initialization is complete, it is not possible to assign a value that differs from the initialization value type.
3. The var requirement is a local variable.
4. Using var to define variables is different from object, which is exactly the same as defining variables in terms of efficiency and using strongly typed methods.

But I suggest that if you know the type of the variable beforehand, use the strongly typed method to declare the variable.

Otherwise, it can make it difficult for developers to determine what type of variable is due to the heavy use of Var. This is not conducive to the maintenance and upgrading of the program.
While Var has pros and cons, if you convert a dynamic language to a C # language, consider using var to define variables.

This is because the dynamic language does not have a type, and to convert it into a strongly typed C # language, you must specify a type for the variable, but it is difficult to determine the type beforehand, rather than specifying it as Var, and then by the C # compiler to determine the specific type of the variable.

What if, in the process of conversion, a variable of dynamic language is found to have changed the type? This can be solved by using the "anonymous class" in the third part of the topic.

C # var declaration variable resolution

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.