[. Net Study Notes] C # new features-implicit type

Source: Internet
Author: User

Blog about implicit type Nocturne StudioArticle(Http://blog.csdn.net/zqustc/article/details/1868276) summed up very well, just use it directly!

Use of implicit type variables

Implicitly typed variables is introduced in C #3.0 ). From my point of view, it is similar to the object type and generic type. It uses a New Keyword VaR to replace the variable type (INT, double, String, etc.) specified in the previous general application in a fixed format ). Let's look at an example:

 
//Explicit Type DefinitionIntMyint =0;BoolMybool =True;StringMystring ="Nocturne studio.";

If the VaR keyword is used for definition, the following format can be written:

//Implicitly typed variables.VaRMyint =0;VaRMybool =True;VaRMystring ="Nocturne studio.";

In this case, the compiler can infer that Myint is of the system. int32 type, and mystring is of the system. string type. You can use the GetType () method to obtain the actual data type.

 Static   Void Main ( String  [] ARGs ){  // Implicitly typed variables.  VaR Myint = 0  ;  VaR Mybool = True  ;  VaR Mystring = "  Nocturne studio.  "  ; Console. writeline (  "  The type of Myint is {0}  " , Myint. GetType (). Name); console. writeline (  "  The type of mybool is {0}  "  , Mybool. GetType (). Name); console. writeline (  "  The type of mystring is {0}  "  , Mystring. GetType (). Name); console. Readline ();} 

The output result is:


In addition, you can assign arrays, generics, and classes to VaR variables, such:

 VaR Evennumbers = New   Int [] {2 , 4 , 6 , 8  };  VaR Mydoubles = New List < Double > ();  //  The Int-type array evennumbers can also execute the foreach statement for iteration.  Foreach ( VaR Item In Evennumbers) {console. writeline (  "  Item value in evennumbers: {0}  "  , Item );}  //  Items in foreach can also be specified with a strong int type.  Foreach ( Int Item In  Evennumbers) {console. writeline (  "  Item value in evennumbers: {0}  "  , Item );} 

Limitations on implicit type variables

There are still some restrictions when using the VaR keyword. First, this type can only be applied to local variables in methods or attributes, but not in fields, function return values, and parameters. For example, the following two operations are invalid:

 Class  Errorexamples {  //  VaR cannot be used in fields  Private   VaR Myint = 10  ;  //  VaR cannot be used in the return value or parameter type.  Public   VaR Mymethod ( VaR X,VaR  Y ){}}  //  The vs2008 editor does not mark VaR as blue to remind youProgramMember. In addition, the local variables declared with the VaR keyword must be assigned a value at the same time and cannot be assigned null.  //  For example, the following operations are all incorrect:  //  Error. The initial value must be assigned.  VaR  Mydata;  //  Error. The value must be assigned at the same time as the declaration.  VaR  Myint; Myint = 0 ;  //  Error. The initial value cannot be null.  VaR Myobj = Null  ;  //  Correct. Point to null after the initial value is assigned.  VaR Mydoubles = New List < Double > (); Mydoubles = Null  ;  // Correct. Use an existing variable to assign the initial value.  VaR Myint = 0  ;  VaR Myanotherint = Myint; the implicit type array can be defined, which is the same as the local variable. The demo is as follows:  //  Actually int []  VaR Arraya = New [] { 1 , 2 , 3 , 4 };  //  It is actually double []  VaR Arrayb = New [] { 1 , 0.5 , 3.14 , 2.71828  };  //  Actually string []  VaR Arrayc = New [] { " Nocturne  " , Null , "  Studio  " , "  Spadeq  "  };  //  Error. The hybrid type is not allowed.  VaR Arrayd = New [] { 1 , " One  " , 2 , "  Two  " };

At present, it seems that I am not very clear about the specific application of this technology, and the abuse of this technology will certainlyCodeThe readability is damaged. Later, I will learn more about the related information of LINQ and should be able to find the answer. Coming soon

 

Note: This article is from Nocturne studio blog article (http://blog.csdn.net/zqustc/article/details/1868276)

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.