你可能不知道的C#语言特性

关键字

  • yield

通常用于迭代器中,向IEnumerable对象提供值或者结束迭代。 如:
yield return expression;
yield break;
  • var

用于定义隐式类型的变量。
var i = 5; 
var s = "Hello";
注意隐式类型(Implicitly typed)并不是“动态类型“,由编译器在编译时候决定具体类型。
default除了在switch语句中提供默认的条件之外,还作为关键字用于泛型中。比如:
T temp = default(T);
由于事先不知道T类型是引用还是值类型,无法用于和null(只有引用类型可以)或者数字(值类型才能和其比较)进行比较。用default关键字,会确保如果是引用类型则返回null,如果是值类型则返回0。
关键字global::用于引用全局命名空间。
class TestClass : global::TestApp { }
用于多线程中表示这个字段可以由多个同时执行的线程修改。更多有关如何使用volatile多线程的例子,可以参考如何:创建和终止线程
public volatile int i;
C#中可以用extern修饰符声明外部实现的方法。常用于Interop服务使用非托管代码与DllImport属性一起使用(同时要声明static),如:
[DllImport("avifil32.dll")]
private static extern void AVIFileInit();
C#中可以同时引用两个类型名完全相同的程序集(常见同一控件的多个版本),这时候使用外部命令行指定别名引用,比如:
/r:GridV1=grid.dll 
/r:GridV2=grid20.dll
在程序中使用它们的时候,需要用关键extern来引用它们:
extern alias GridV1; 
extern alias GridV2;
GridV1和GridV2会被引用进和全局命名空间同级别的额外空间,使用GridV1::Grid或者GridV2::Grid可以得到不同版本的Grid类型。

 

语法

  • ??操作符

null合并运算符,用于定义可以为null值的引用类型的默认值。如果左操作数不为null,则返回左操作数,反之返回右操作数。
 int y = x ?? -1;
同时这里也是null运算符:
static int? GetNullableInt()
{
    return null;
}
//... or
int? x = null;
  • where T:new()

我们知道可以用where关键字来对泛型定义进行约束,比如:
public class MyClass<T, U>
    where T : class
    where U : struct
{}
同时我们可以对泛型定义中包含构造函数的约束,new()约束告诉编译器提供的任何类型的参数都含有一个无参数的(默认)构造方法。
public class MyGenericClass<T> where T : IComparable, new()
{
    T item = new T();
}

语言特性

  • Nullable类型

REXML could not parse this XML/HTML: 
<blockquote>当我们要描述一个值类型可以不存在的的时候,使用nullable类型取代值类型。

使用?修饰符来声明一个nullable类型,比如:</blockquote>

int? a = 1;
C#通过Nullable类和Nullable结构体来支持nullable类型,Nullable结构体含有两个有用属性HasValueValue来判断和获取当前类型是否还有值。
REXML could not parse this XML/HTML: 
<blockquote>这个名词对.NET的朋友可能有点生疏,<a href="http://zh.wikipedia.org/wiki/%E6%9F%AF%E9%87%8C%E5%8C%96" target="_blank">wikipedia</a>给的翻译是“柯里化”- 个人不太喜欢这个名字,英文是<a href="http://en.wikipedia.org/wiki/Currying" target="_blank">currying</a>。curry化是函数式编程的一种实现技术。大致过程,就是把本来接受多个参数的函数,变成只有第一个参数的函数,然后返回新函数接受余下的参数。有点拗口,而且对函数式编程或curry化介绍的中文资料也相对较少,我读过<a href="http://twurl.nl/jcn2pj" target="_blank">一篇对函数式编程和curry化解释的比较清楚的文章</a>还有这篇<a href="http://diditwith.net/2007/08/15/TheArtOfCurrying.aspx" target="_blank">英文的博客</a>,推荐大家看看。

所有能实现闭包的语言都可以实现curry化。在C#2.0中可以通过匿名委托来实现,在3.0中curry化相对简单一些:</blockquote>

REXML could not parse this XML/HTML: 
<blockquote>
<pre>static class Program
{
  static Func&lt;TArg1, Func&lt;TArg2, TResult&gt;&gt; Curry&lt;TArg1, TArg2, TResult&gt;(this Func&lt;TArg1,
      TArg2, TResult&gt; f)  {
    return a1 =&gt; a2 =&gt; f(a1, a2);
  }

static void Main() { Func<int, int, int> add = (x, y) => x + y; var curriedAdd = add.Curry(); Console.WriteLine(curriedAdd(13)(29)); } }</pre> </blockquote>

最后,如果大家对用C#实现函数式编程有兴趣,可以在MSDN Code中找到更多的代码实现
  • 匿名类型

匿名类型可用来将一组只读属性封装到单个对象中,而无需首先显式定义一个类型。类型名称由编译器生成,并且不能在源代码级使用,类型也由编译器推断决定。常和LINQ查询表达式的select子句结合使用,初始化其他类型的属性组成的对象。
var productQuery = 
    from prod in products
    select new { prod.Color, prod.Price }; //创建由Color和Price属性组成的新对象
foreach (var v in productQuery)
{
    Console.WriteLine("Color={0}, Price={1}", v.Color, v.Price);
}
  • 扩展方法

    C#3.0中可以对CLR扩充类型,比如,你可能需要得到一句英文字符串中有多少个单词,以往可以写一个处理string的方法,现在你可以直接为string类型添加一个计算单词数量的方法:
REXML could not parse this XML/HTML: 
<blockquote>
<pre>namespace ExtensionMethods
{
    public static class MyExtensions
    {
        public static int WordCount(this String str)
        {
            return str.Split(new char[] { ' ', '.', '?' }, StringSplitOptions.RemoveEmptyEntries).Length;
        }
    }   
}</pre>
&nbsp;

通过this我们把这个WordCount方法扩充到了CLR内置类型,你可以直接像用普通方法一样使用它:</blockquote>

REXML could not parse this XML/HTML: 
<blockquote>
<pre>using ExtensionMethods;
//...

string s = “Hello Extension Methods”; int i = s.WordCount();</pre> </blockquote>

更多介绍可以参考MSDN或者ScottGu的博客。
 

方法和属性

  • List.ForEach()

Array或List有个很实用的ForEach方法,可以直接传入一个方法对集合中元素操作。如:
REXML could not parse this XML/HTML: 
<blockquote>
<pre>List&lt;String&gt; names = new List&lt;String&gt;();
names.Add("Bruce");
names.Add("Alfred");

names.ForEach(Print); names.ForEach(delegate(String name) { Console.WriteLine(name); });

private static void Print(string s) { Console.WriteLine(s); }</pre> </blockquote>

  • GetValueOrDefault

REXML could not parse this XML/HTML: 
<blockquote>对于nullable类型的对象,除了Value和HasValue两个常用属性外,还可以使用GetValueOrDefault方法来获得当前值或者默认值:
<pre>float? mySingle = 12.34f;
float? yourSingle = -1.0f;

yourSingle = mySingle.GetValueOrDefault(-222.22f); // yourSingle=12.34

mySingle = null; yourSingle = mySingle.GetValueOrDefault(); // yourSingle=0</pre> </blockquote>  

技巧

  • 使用大写字母比较字符串

摘自CLR via C#:
当比较字符串时候,推荐使用ToUpperInvariant 转换成大写而不是ToLowerInvariant 成小写。因为微软在比较大写字符串的时候对代码做了优化。