與時俱進的語言
一、System Class
1. 泛型(genric)
- 可更清楚的描述序列中每個元素的類型。
C#1 示例
public class Bookshelf
{
public IEnumerable Books { get { ... } }
}
C#2 示例:泛型
public class Bookshelf
{
public IEnumerable<Book> Books { get { ... } }
}
2. 可空值類型(nullable value type)
- 可有效的表示未定的變量值,以擺脫魔數(用
-1
當集合索引,用MinValue
或MaxValue
做為初始值)。
示例
string? a = null;
Console.WriteLine(a ?? "null"); // null
a = "abc";
Console.WriteLine(a ?? "null"); // abc
3. 匿名類型(anonymous type)、隱式局部變數(var)
- 兩者皆可解決靜態類型語言的缺陷:程式碼冗長。
示例1: 匿名類型(anonymous type)
var book = new
{
Title = "Harry Potter",
Author = "J.K. Rowling"
}
string title = book.Title;
string author = book.Author;
- 若已經調用了建構式的話,就無需顯式的宣該告變數的類型了。
示例2: 隱式類型(implicit typing)
Dictionary<string, string> map1 = new Dictionary<string, string>();
var map2 = new Dictionary<string, string>();
4. 簡潔化
示例1:委托
button.Click += new EventHandler(HandleButtonClick); // C#1
button.Click += HandleButtonClick; // C#2 方法組轉換+匿名方法
button.Click += delegate { MessageBox.Show("Clicked!") }; // C#2 委托
button.Click += (sender, args) => MessageBox.Show("Clicked!"); // C#3 lamnda 表達式
void HandleButtonClick(object sender, EventArgs args)
{
MessageBox.Show("Clicked!");
}
示例2:自動實現
下面兩段 code 可以經由自動實現視為相同
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
public string Name { get; set; }
示例3:表達式主體成員
下面兩段 code 可以經由表達式主體成員(expression-bodied member)視為相同
public int Count { get { return list.Count; } }
public IEnumerator<string> GetEnumerator()
{
return list.GetEnumerator();
}
public int Count => list.Count;
public IEnumerator GetEnumerator => list.GetEnumerator();
示例4:字串處理:內插內串字面量(interpolated string literal)
throw new KeyNotFoundException("No calendar system for Id " + id + " exists"); // 字串拼接
throw new KeyNotFoundException(string.Format("No calendar system for Id {0} exists", id)); // 字串格式化
throw new KeyNotFoundException($"No calendar system for Id {id} exists"); // 內插字串字面量
示例5: 利用LINQ進行數據訪問
var offers =
from product in db.Products
where product.SalePrice <= product.Price / 2
orderby product.SalePrice
select new {
producdt.Id,
product.Description,
product.SalePrice,
priduct.Price
};
示例6: 非同步
private async Task UpdateStatus()
{
Task<Weather> weatherTask = GetWeatherAsync();
Task<EmailStatus> emailTask = GetEmailStatusAsync();
Weather weather = await weatherTask;
EmailStatus emai = await emailTask;
weatherLabel.Text = weather.Description;
inboxLabel.Text = email.InboxCount.ToString();
}