一、基本链式格式

    class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
Db person = new Db { Name = "Tom" }
.Run()
.Sing()
.Swim();
}
public class Db
{
public string Name { get; set; }
public Db Run()
{
Console.WriteLine("Run");
return this;
}
public Db Swim()
{
Console.WriteLine("Swim");
return this;
}
public Db Sing()
{
Console.WriteLine("Sing");
return this;
}
}
}

链式编程是将多个操作(多行代码)通过点号"."链接在一起成为一句代码。(定义不严密,表达能力好的请给修正一下)以下为常规代码与链式代码的比较:

/// <summary>
/// 链式编程示例3 对比
/// </summary>
private void Example3()
{
//链式代码
Person person = new Person { Name = "Tom" }
.Run()
.Sing()
.Swim()
.Run()
.Sing()
.Swim();
//常规代码
Person person2 = new Person();
person2.Run();
person2.Sing();
person2.Swim();
person2.Run();
person2.Sing();
person2.Swim();
}

二、

05-11 22:11