对象初始化表达式在构造函数中的应用:
using System;
using System.Collections.Generic;
using System.Text;
namespace Sample
{
class Program
{
static viod Main()
{
//使用Exception类的过早函数实例化一个Exception对象
throw new exception("MyException"){ Source="LINQ Stduy"};
}
}
}
相同的逻辑但不使用对象初始化表达式
static viod Main()
{
//使用Exception类的构造函数实例化一个Exception对象
Exception exception = new Exception ("MyException");
exception.Source=" LINQ Study ";
throw exception;
}