10 little-known C # keywords

Source: Internet
Author: User

Before getting started, I need to declare that these keywords are more familiar to programmers who prefer the underlying layer. Not understanding these keywords does not affect you as a qualified programmer.

This means that these keywords will give you better code quality and readability when writing a program, enjoy

Yield

The yield keyword tells the compiler that the current function is in a loop, and the compiler generates a class that executes the behavior it represents inside the loop body, the yield and return keywords are used together to provide the return value for the enumerated object. For example, in each loop within foreach, the yield keyword is used to terminate the current loop:

 

      public classList   {        //using System.Collections;       public static IEnumerable Power(int number, int exponent)        {            int counter = 0;            int result = 1;            while(counter++ < exponent)            {                result = result * number;                yield return result;            }        }        static void Main()        {            // Display powers of 2 up to the exponent 8:           foreach (int i in Power(2, 8))            {                Console.Write("{0} ", i);            }        }    }    /*    Output:    2 4 8 16 32 64 128 256    */

Msdn link: http://msdn.microsoft.com/en-us/library/9k7k7cf0.aspx

 

VaR

Since C #3.0, variables declared within the scope of function functions can be declared as implicit types by using the VaR keyword. Implicit types are strongly typed. You need to declare local variables of implicit types by yourself, then the compiler will help you decide a type of strong type.

Programs running on version 2.0 can also use the VaR keyword, but your compiler must be of version 3.0 or later and set the code output version to 2.0:

var i = 10; // implicitly typedint i = 10; //explicitly typed

Msdn link: http://msdn.microsoft.com/en-us/library/bb383973.aspx

 

Using ()

Define a range. objects out of the range will be recycled:

using (C c = new C()){    c.UseLimitedResource();}

Msdn link: http://msdn.microsoft.com/en-us/library/yh598w02%28VS.80%29.aspx

 

Readonly

The readonly keyword is a modifier that can be used in the variable field. When a variable field is modified by readonly, this variable can only be assigned to a value in the Declaration or constructor of the class to which the current variable belongs.

Msdn link: http://msdn.microsoft.com/en-us/library/acdd6hb7%28VS.80%29.aspx

 

As

The as operator is similar to a type converter. However, when the conversion fails (the translator presses: for example, the type does not match), as returns NULL instead of throwing an exception:

class Class1{ }classClass2{ }classClass3: Class2{ }classIsTest   {        static voidTest(objecto)        {            Class 1a;            Class 2b;            if(o isClass1)            {                Console.WriteLine("o is Class1");                a = (Class1)o;                // Do something with "a."            }            else if (o is Class2)            {                Console.WriteLine("o is Class2");                b = (Class2)o;                // Do something with "b."            }            else            {                Console.WriteLine("o is neither Class1 nor Class2.");            }        }        static void Main()        {            Class1 c1 = new Class1();            Class2 c2 = new Class2();            Class3 c3 = new Class3();            Test(c1);            Test(c2);            Test(c3);            Test("a string");        }    }    /*    Output:    o is Class1    o is Class2    o is Class2    o is neither Class1 nor Class2.    */

 

Msdn link: http://msdn.microsoft.com/en-us/library/scekt9xw.aspx

 

Default

One problem that occurs in generic classes and generic methods is how to assign the default value to parameterized type T when the following conditions are unknown:

  • T is the reference type or value type.

  • If t is of the value type, whether it is a value or a structure.

Given a variable t of the parameterized type T, the Statement t = NULL is valid only when T is of the reference type; only when T is of the numerical type rather than the structure, statement t = 0 can be used normally. The solution is to use the default keyword, which returns NULL for the reference type and zero for the value type. For the structure, this keyword returns each structure member whose Initialization is zero or null, depending on whether the structure is a value type or a reference type:

T temp = default(T);

Msdn link: http://msdn.microsoft.com/en-us/library/xwth0h0d.aspx

 

Global

In: The global context keyword used before the operator references the global namespace, which is the default namespace of any C # program and is not named in other ways.

class TestClass : global::TestApp { }

Msdn link: http://msdn.microsoft.com/en-us/library/cc713620.aspx

Volatile

The volatile keyword indicates that the field may be modified by multiple concurrent execution threads. Fields declared as volatile are not restricted by Compiler Optimization (assuming that they are accessed by a single thread. This ensures that the field displays the latest value at any time.

Msdn link: http://msdn.microsoft.com/en-us/library/x13ttww7%28VS.80%29.aspx

 

Extern alias

Sometimes it is necessary to reference two versions of an assembly with the same fully qualified type name, for example, when two or more versions of the Assembly need to be used in the same application. By using the external Assembly alias, the namespaces from each assembly can be packaged in the root-level namespace named by the alias, so that they can be used in the same file.

To reference two assembly with the same fully qualified type name, you must specify the alias on the command line, as shown below:

/R: gridv1 = grid. dll

/R: gridv2 = grid1_dll

This will create an external aliasGridv1AndGridv2. To use these aliases from a program, use the extern keyword to reference them. For example:

Extern alias gridv1;

Extern alias gridv2;

Each external alias declaration introduces an additional root-level namespace, which is parallel to the global namespace, rather than within the global namespace. Therefore, the types from each assembly can be referenced by their own fully qualified names that are rooted in the appropriate namespace alias, without the ambiguity.

In the preceding example,Gridv1: GridYes fromGrid. dllAndGridv2: GridYes fromGridw.dllGrid Control.

 

Msdn link: http://msdn.microsoft.com/en-us/library/ms173212%28VS.80%29.aspx

 

 

---------------------------------------

Link: http://hatim.indexdev.net/2009/12/08/10-not-so-well-known-keywords-in-c/

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.