C# 新功能

C# 1.0

.NET Framework 版本

  • .NET Framework 1.0

Visual Studio 版本

  • Visual Studio .NET 2002

CLR 版本

  • CLR 1.0

C# 1.2

.NET Framework 版本

  • .NET Framework 1.1

Visual Studio 版本

  • Visual Studio .NET 2003

CLR 版本

  • CLR 1.1

C# 2.0

.NET Framework 版本

  • .NET Framework 2.0

Visual Studio 版本

  • Visual Studio 2005

CLR 版本

  • CLR 2.0

新特性

無泛型寫法

public void WithoutGenerics()
{
    ArrayList list = new ArrayList();

    // ArrayList is of type object, therefore essentially untyped.
    // Results in boxing and unboxing of value types
    // Results in ability to mix types which is bad practice.
    list.Add(1);
    list.Add("foo");
}

泛型寫法

public void WithGenerics()
{
    // Generics provide for strongly typed collections.
    List<int> list = new List<int>();
    list.Add(1); // allowed
    // list.Add("foo"); // not allowed
}
// in Employee1.cs
public partial class Employee
{
    public void DoWork()
    {
    }
}

// in Employee2.cs
public partial class Employee
{
    public void GoToLunch()
    {
    }
}

舊版寫法

public class Holloween
{
    public event EventHandler ScareMe;

    public void OldBoo()
    {
        ScareMe += new EventHandler(DoIt);
    }

    public void Boo()
    {
        ScareMe(this, EventArgs.Empty);
    }

    public void DoIt(object sender, EventArgs args)
    {
        Console.WriteLine("Boo!");
    }
}

新版寫法

public void NewBoo()
{
    ScareMe += delegate (object sender, EventArgs args) { Console.WriteLine("Boo!"); };
}

public void AsyncBoo()
{
    new Thread(delegate () { Console.WriteLine("Boo!"); }).Start();
}
  • Iterators/迭代器

舊版寫法,需實作 CurrentMoveNextReset

public class DaysOfWeekOld : IEnumerable
{
    protected string[] days = new string[] {
        "Monday", "Tuesday", "Wednesday", "Thursday",
        "Friday", "Saturday", "Sunday" };

    public int Count { get { return days.Length; } }
    public string this[int idx] { get { return days[idx]; } }

    public IEnumerator GetEnumerator()
    {
        return new DaysOfWeekEnumerator(this);
    }
}

public class DaysOfWeekEnumerator : IEnumerator
{
    protected DaysOfWeekOld dow;
    protected int pos = -1;

    public DaysOfWeekEnumerator(DaysOfWeekOld dow)
    {
        this.dow = dow;
    }

    public object Current
    {
        get { return dow[pos]; }
    }

    public bool MoveNext()
    {
        ++pos;
        return (pos < dow.Count);
    }

    public void Reset()
    {
        pos = -1;
    }
}

新版寫法

public class DaysOfWeekNew : IEnumerable
{
    protected string[] days = new string[] {
        "Monday", "Tuesday", "Wednesday", "Thursday",
        "Friday", "Saturday", "Sunday" };

    public IEnumerator GetEnumerator()
    {
        for (int i = 0; i < days.Length; i++)
        {
            yield return days[i];
        }
    }
}
  • Nullable types

  • Getter/setter separate accessibility

  • Method group conversions (delegates)

  • Co- and Contra-variance for delegates

  • Static classes

  • Delegate inference

C# 3.0

.NET Framework 版本

  • .NET Framework 2.0
  • .NET Framework 3.0
  • .NET Framework 3.5

Visual Studio 版本

  • Visual Studio 2008
  • Visual Studio 2010

CLR 版本

  • CLR 2.0

新特性

  • Implicitly typed local variables

  • Object and collection initializers

  • Auto-Implemented properties

  • Anonymous types

  • Extension methods

  • Query expressions

  • Lambda expressions

  • Expression trees

  • Partial methods

C# 4.0

.NET Framework 版本

  • .NET Framework 4

Visual Studio 版本

  • Visual Studio 2010

CLR 版本

  • CLR 4

新特性

  • Dynamic binding

  • Named and optional arguments

  • Generic co- and contravariance

  • Embedded interop types ("NoPIA")

C# 5.0

.NET Framework 版本

  • .NET Framework 4.5

Visual Studio 版本

  • Visual Studio 2012
  • Visual Studio 2013

CLR 版本

  • CLR 4

新特性

  • Asynchronous methods

  • Caller info attributes

C# 6.0

.NET Framework 版本

  • .NET Framework 4.6 (.NET 2015)

Visual Studio 版本

  • Visual Studio 2015

CLR 版本

  • CLR 4

新特性

  • Compiler-as-a-service (Roslyn)

  • Import of static type members into namespace

  • Exception filters

  • Await in catch/finally blocks

  • Auto property initializers

  • Default values for getter-only properties

  • Expression-bodied members

  • Null propagator (null-conditional operator, succinct null checking)

  • String Interpolation

舊版寫法,使用 string.Format

var name = "Bruce";
var welcome = string.Format("Hi, {0}", name);
var now = string.Format("It's {0:yyyy/MM/dd}", DateTime.Now);

新版寫法

var name = "Bruce";
var welcome = $"Hi, {name}";
var now = $"It's {DateTime.Now:yyyy/MM/dd}";
public void Run(string opertion)
{
    if (string.IsNullOrWhiteSpace(opertion))
        throw new ArgumentException(nameof(opertion) + " is invalid");

    // do something
}
    • Dictionary initializer

Reference

results matching ""

    No results matching ""