The var keyword in C,

Source: Internet
Author: User

The var keyword in C,

Var is a new definition variable of 3.5. In fact, it is also a weakening type definition. VAR can replace any type. The compiler will judge what type you want to use based on context. when using VAR, I think that you cannot determine what type you will use. You can use VAR similar to an OBJECT, but the efficiency is higher than that of the OBJECT.

Or in other words:

Var can be understood as an anonymous type. We can think of it as a placeholder for declaring variables. It is mainly used when variables are declared and data types cannot be determined.

 

Using var to define variables has the following four features:

1. It must be initialized during definition. That is, it must be in the form of var s = "abcd", rather than the following: var s; s = "abcd ";

2. Once Initialization is complete, the variable cannot be assigned a value of a different type than the initialization value.

3. var must be a local variable.

4. Using var to define variables is different from object. It is the same in efficiency as defining variables using a strong type.

 

Instance:

Using System; using System. collections. generic; using System. linq; using System. text; using System. threading. tasks; namespace FirstPriject {class Program {static void Main (string [] args) {// declare and initialize the variable var name = "Zhang San"; var age = 10; var sex = true; // Type t_Name = name of the retrieved variable. getType (); Type t_Age = age. getType (); Type t_Sex = sex. getType (); // print the result Console. writeLine ("the variable name type is {0}, the variable age type is {1}, and the variable sex type is {2}", t_Name.ToString (), t_Age.ToString (), t_Sex.ToString (); // The Console is not automatically closed, waiting for the Console to be entered. readLine ();}}}
View Code

 

Instance result diagram:

 

This article cited from: http://wenda.so.com/q/1378644256068714

This article takes notes based on <VS2010 easy to learn C #-from scratch to deep-NET4 fun programming video tutorial> In the Baidu course.

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.