10个鲜为人知的C#关键字

简介:

10 Not So Well Known Keywords in C#

Ok before the flaming start let me state this. These are known to most hardcore programmers and not knowing them doesn’t make you less of a programmer either.

That said these keywords can come in handy and allow for better code quality and readability. Enjoy!

yield

The yield keyword signals to the compiler that the method in which it appears is an iterator block. The compiler generates a class to implement the behavior that is expressed in the iterator block. In the iterator block, the yield keyword is used together with the return keyword to provide a value to the enumerator object. This is the value that is returned, for example, in each loop of a foreach statement. The yield keyword is also used with break to signal the end of iteration.

example:

  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 returnresult; 
           } 
       }

       static void Main() 
       { 
           // Display powers of 2 up to the exponent 8: 
          
foreach (int inPower(2, 8)) 
           { 
               Console.Write("{0} ", i); 
           } 
       } 
   } 
   /* 
   Output: 
   2 4 8 16 32 64 128 256 
   */

 

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

var

Beginning in Visual C# 3.0, variables that are declared at method scope can have an implicit type var. An implicitly typed local variable is strongly typed just as if you had declared the type yourself, but the compiler determines the type.

An application running on version 2.0 can also use the var keyword in their code as long as it’s compiled with 3.0 or up and set to output to 2.0.

example:

var i = 10; // implicitly typed

int i = 10; //explicitly typed

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

using()

Defines a scope, outside of which an object or objects will be disposed.

example:

using (C c = new C())

{

    c.UseLimitedResource();

}

 

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

readonly

The readonly keyword is a modifier that you can use on fields. When a field declaration includes a readonlymodifier, assignments to the fields introduced by the declaration can only occur as part of the declaration or in a constructor in the same class.

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

as

The as operator is like a cast operation. However, if the conversion is not possible, as returns null instead of raising an exception. 

example:

class Base

    
public override string ToString()

        {

            return "Base";

        }

    }

    class Derived Base

    
{ }

    class Program

    
{

        static void Main()

        {

            Derived d = new Derived();

            Base b = d as Base;

            if (b != null)

            {

                Console.WriteLine(b.ToString());

            }

        }

    }

 

msdn reference: http://msdn.microsoft.com/en-us/library/cscsdfbt.aspx

is

Checks if an object is compatible with a given type.

An is expression evaluates to true if the provided expression is non-null, and the provided object can be cast to the provided type without causing an exception to be thrown.

The is keyword causes a compile-time warning if the expression is known to always be true or to always be false, but typically evaluates type compatibility at run time.

The is operator cannot be overloaded.

example:

class Class1{ }

classClass2{ }

classClass3Class2{ }

classIsTest

   
{

        static voidTest(objecto)

        {

            Class1a;

            Class2b;

            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 reference: http://msdn.microsoft.com/en-us/library/scekt9xw.aspx

default

 

  •  

    n generic classes and methods, one issue that arises is how to assign a default value to a parameterized type T when you do not know the following in advance:

  • Whether T will be a reference type or a value type.

  • If T is a value type, whether it will be a numeric value or a struct.

Given a variable t of a parameterized type T, the statement t = null is only valid if T is a reference type and t = 0 will only work for numeric value types but not for structs. The solution is to use the default keyword, which will return null for reference types and zero for numeric value types. For structs, it will return each member of the struct initialized to zero or null depending on whether they are value or reference types. For nullable value types, default returns a System.Nullabe<T>, which is initialized like any struct.

example:

T temp = default(T);

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

global

The global contextual keyword, when it comes before the :: operator, refers to the global namespace, which is the default namespace for any C# program and is otherwise unnamed.

example:

class TestClass global::TestApp { }

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

volatile

The volatile keyword indicates that a field might be modified by multiple concurrently executing threads. Fields that are declared volatile are not subject to compiler optimizations that assume access by a single thread. This ensures that the most up-to-date value is present in the field at all times.

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

extern alias

It can sometimes be necessary to reference two versions of assemblies that have the same fully-qualified type names, for example when you need to use two or more versions of an assembly in the same application. By using an external assembly alias, the namespaces from each assembly can be wrapped inside root-level namespaces named by the alias, allowing them to be used in the same file.

To reference two assemblies with the same fully-qualified type names, an alias must be specified on the command line, as follows:

/r:GridV1=grid.dll

/r:GridV2=grid20.dll

This creates the external aliases GridV1 and GridV2. To use these aliases from within a program, reference them using the extern keyword. For example:

extern alias GridV1;

extern alias GridV2;

Each extern alias declaration introduces an additional root-level namespace that parallels (but does not lie within) the global namespace. Thus types from each assembly can be referred to without ambiguity using their fully qualified name, rooted in the appropriate namespace-alias

In the above example, GridV1::Grid would be the grid control from grid.dll, and GridV2::Grid would be the grid control from grid20.dll.

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

 

Hope this helps!

Hatim

 

在正式开始之前,我需要先声明:这些关键字对于偏向底层的程序员更加耳熟能详,对这些关键字不了解并不影响你作为一个合格的程序员。

  这意味着这些关键字会让你在编写程序时得到更好的代码质量和可读性

  yield

  yield关键字会告诉编译器当前的函数是在一个循环内部,编译器会相应生成一个执行它在循环体内部所表示行为的类,yield和return关键字一起用于为枚举器对象提供返回值,比如说:在foreach内部的每一次循环内,yield关键字用于终止当前循环:

   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链接:http://msdn.microsoft.com/en-us/library/9k7k7cf0.ASPx

  var

  自从C# 3.0开始,在函数作用局范围内声明的变量可以通过var关键字声明成隐含类型,隐含类型是强类型,你需要自己声明隐含类型本地变量,然后编译器会帮你决定为某种强类型。

在2.0版本上跑的程序也可以使用var关键字,但是需要你的编译器是3.0以上版本并且设置代码输出版本为2.0:

var i = 10; // implicitly typed 

int i = 10; //explicitly typed

  MSDN链接:http://msdn.microsoft.com/en-us/library/bb383973.ASPx

  using()

  定义一个范围,在范围外的对象将会被回收:

using (C c = new C()) 

{ 

c.UseLimitedResource(); 

}

  MSDN链接:http://msdn.microsoft.com/en-us/library/yh598w02%28VS.80%29.aspx

  readonly

  readonly关键字是一个可作用在变量域上的修饰符,当一个变量域被readonly修饰后,这个变量只可在声明或者当前变量所属类的构造器内赋值。

  MSDN链接:http://msdn.microsoft.com/en-us/library/acdd6hb7%28VS.80%29.aspx

  as

  as操作符很像一个类型转换器,然和,当转换无法发生时(译者按:比如类型不匹配),as会返回null而不是抛出一个异常:

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链接:http://msdn.microsoft.com/en-us/library/scekt9xw.ASPx

  default

  在泛型类和泛型方法中产生的一个问题是,在预先未知以下情况时,如何将默认值分配给参数化类型 T:

  T 是引用类型还是值类型。

  如果 T 为值类型,则它是数值还是结构。

  给定参数化类型 T 的一个变量 t,只有当 T 为引用类型时,语句 t = null 才有效;只有当 T 为数值类型而不是结构时,语句 t = 0 才能正常使用。解决方案是使用 default 关键字,此关键字对于引用类型会返回 null,对于数值类型会返回零。对于结构,此关键字将返回初始化为零或 null 的每个结构成员,具体取决于这些结构是值类型还是引用类型:

T temp = default(T);

  MSDN链接:http://msdn.microsoft.com/en-us/library/xwth0h0d.aspx

  global

  在  ::运算符前面使用的 global 上下文关键字引用全局命名空间,该命名空间是任何 C# 程序的默认命名空间,未以其他方式命名。

class TestClass : global::TestApp { }

  MSDN链接:http://msdn.microsoft.com/en-us/library/cc713620.aspx

  volatile

  volatile 关键字表示字段可能被多个并发执行线程修改。声明为volatile 的字段不受编译器优化(假定由单个线程访问)的限制。这样可以确保该字段在任何时间呈现的都是最新的值。

  MSDN链接:http://msdn.microsoft.com/en-us/library/x13ttww7%28VS.80%29.aspx

  extern alias

  有时可能有必要引用具有相同完全限定类型名的程序集的两个版本,例如当需要在同一应用程序中使用程序集的两个或更多的版本时。通过使用外部程序集别名,来自每个程序集的命名空间可以在由别名命名的根级别命名空间内包装,从而可在同一文件中使用。

 

  若要引用两个具有相同完全限定类型名的程序集,必须在命令行上指定别名,如下所示:

  /r:GridV1=grid.dll

  /r:GridV2=grid20.dll

  这将创建外部别名 GridV1 和 GridV2。若要从程序中使用这些别名,请使用 extern 关键字引用它们。例如:

  extern alias GridV1;

  extern alias GridV2;

  每一个外部别名声明都引入一个额外的根级别命名空间,它与全局命名空间平行,而不是在全局命名空间内。因此,来自每个程序集的类型就可以通过各自的、根源于适当的名空间别名的完全限定名来引用,而不会产生多义性。

  在上面的示例中,GridV1::Grid 是来自 grid.dll 的网格控件,而 GridV2::Grid 是来自 grid20.dll 的网格控件。

  MSDN链接:http://msdn.microsoft.com/en-us/library/ms173212%28VS.80%29.aspx
















本文转蓬莱仙羽51CTO博客,原文链接:http://blog.51cto.com/dingxiaowei/1366159,如需转载请自行联系原作者

相关文章
|
2月前
|
C#
30.C# 关键字 this初步学习
30.C# 关键字 this初步学习
15 1
|
2月前
|
C#
28.c#关键字base初步学习
28.c#关键字base初步学习
12 0
|
2月前
|
C#
27.c#关键字sealed修饰类
27.c#关键字sealed修饰类
12 0
|
3月前
|
SQL 开发框架 .NET
EntityFramework数据持久化复习资料3、C#拓展方法与yield关键字使用
EntityFramework数据持久化复习资料3、C#拓展方法与yield关键字使用
25 0
|
4月前
|
开发框架 .NET 编译器
C# 9.0中的静态匿名函数:引入static关键字的新用法
【1月更文挑战第15天】C# 9.0为匿名函数带来了一个新的修饰符static,允许开发者明确指定匿名函数不会捕获其包含作用域中的任何变量。这一特性增强了代码的性能和可读性,同时减少了因不小心捕获变量而导致的潜在错误。本文将详细探讨C# 9.0中静态匿名函数的语法、使用场景以及它们如何影响代码的性能和安全性。
|
2月前
|
存储 Java C++
31.C#:关键字static
31.C#:关键字static
15 1
|
2月前
|
C#
29.C#关键字throw初步学习
29.C#关键字throw初步学习
23 0
|
3月前
|
存储 编译器 C#
C#关键字常见面试题
C#关键字常见面试题
|
8月前
|
存储 编译器 C#
C#关键字相关面试题
C#关键字相关面试题
|
4月前
|
C# 数据安全/隐私保护 开发者
C# 9.0中的Init关键字:Init-only Setters的新篇章
【1月更文挑战第12天】本文介绍了C# 9.0中引入的Init关键字,该关键字允许创建仅在对象初始化时可设置属性的setter。通过Init-only setters,开发者能够更加灵活地控制对象属性的赋值时机,提高代码的可维护性和安全性。文章详细解释了Init关键字的使用方法、适用场景以及与传统setter的区别,并探讨了其在实际开发中的潜在影响。