隨著 Visual Studio 2017 即將正式釋出,C# 7.0 也隨之來到。身為這次的改版,將會帶來許多方便的新功能
if (int.TryParse(input, out var answer))
WriteLine(answer);
else WriteLine("Could not parse input");
一起讓我們來看看 C# 7.0 的威力吧。
5. 5
//二進位數字表示法
int i = 0b0010;
Console.WriteLine(i);
// 使用底線讓數字更清晰
int j = 100_000;
Console.WriteLine(j);
int k = 1_00;
Console.WriteLine(k);
int l = 0b0010_0000;
Console.WriteLine(l);
double d = 3.141_592_653;
Console.WriteLine(d);
10. 在 switch 上加入 when
10
private static void Execute(object value)
{
switch (value)
{
case int x when x > 0 && x < 100:
Console.WriteLine($"x 是小整數 : {x}");
break;
case int x when x > 99 && x < 1000:
Console.WriteLine($"x 是大整數 : {x}");
break;
case int x:
Console.WriteLine($"x 超出範圍");
break;
case string x:
Console.WriteLine($"x 是字串 : {x}");
break;
default:
Console.WriteLine("不在 case 內");
break;
}
}
11. http://mvc.tw
is 其實是 as
結構型別會換成 Nullable<T>
switch 和 when 會被 if 取代
容易造成方法複雜度增加
編譯器玩了甚麼魔術
11
12. http://mvc.tw
在 C# 6.0(包含) 以前的
is
會對變數只少做兩次存取
as
無法處理不可為 null 的型別
當變數值為 null 的時候,無法分辨究竟是轉型失敗還是傳
進來的值就是 null
過去的 is 和 as 有甚麼缺點
12
13. http://mvc.tw
在 C# 6.0 開始出現此形式的寫法。本來只用在 method
和 read only property。
C# 7.0 將這個形式擴張到
constructor
finalizer
getter and setter on property
indexer
更狂的 expression-bodied members
13
14. 14
public class MyCircle
{
private double _radius;
private string _name;
public double Radius
{
get => _radius;
set => this._radius = value;
}
public string Name
{
get => _name;
set => this._name = value ?? "就是這個圓";
}
public MyCircle() => _radius =2;
public double GetArea() =>
(_radius > 0) ? Math.PI * (Math.Pow(_radius, 2)) : 0;
}
16. 16
public class MyCircle
{
private double _radius;
private string _name;
public double Radius
{
get => _radius;
set => _radius = value > 0 ? value : throw new ArgumentException();
}
public string Name
{
get => _name;
set => this._name = value ?? throw new ArgumentException();
}
}
23. 23
string s="123";
// 以前你要這樣寫
int i;
int.TryParse(s,out i);
Console.WriteLine(i);
// 現在你可以直接這樣寫
int.TryParse(s, out int j);
Console.WriteLine(j);
// ref 不能比照 out 使用
//Test(ref int y = 10);
29. 建立 Deconstruct 執行個體方法
29
class Program
{
static void Main(string[] args)
{
MyRectangle rect = new MyRectangle() { Width = 10, Height = 30 };
(int x, int y)= rect;
Console.WriteLine($"{x} -- {y}");
Console.ReadLine();
}
}
public class MyRectangle
{
public int Width { get; set; }
public int Height { get; set; }
public void Deconstruct(out int width, out int height)
{
width = this.Width;
height = this.Height;
}
}
30. Deconstruct 也可以用擴充方法形式
30
public class MyRectangle
{
public int Width { get; set; }
public int Height { get; set; }
}
public static class MyExtension
{
public static void Deconstruct
(this MyRectangle rect, out int width, out int height)
{
width = rect.Width;
height = rect.Height;
}
}
31. Deconstruct 一個有趣的小地方
31
MyRectangle rect = new MyRectangle() { Width = 5, Height = 60 };
(int x, int y) = rect;
Console.WriteLine($"{x} -- {y}");
Console.ReadLine();
(new MyRectangle() { Width = 5, Height = 60 })
.Deconstruct(out int x, out int y);
Console.WriteLine($"{x} -- {y}");
Console.ReadLine();
33. 以前要操作變數指標是一件麻煩事
33
static void Main(string[] args)
{
int number = 100;
unsafe
{
int* p = &number;
Console.WriteLine(*p);
*p = 999;
Console.WriteLine(number);
}
Console.ReadLine();
}
34. 現在你只要這麼做就行了
34
static void Main(string[] args)
{
int number = 100;
ref int p = ref number;
Console.WriteLine(p);
p = 999;
Console.WriteLine(number);
Console.ReadLine();
}
35. 陣列處理 – 過去
35
static void Main(string[] args)
{
string[] data = new string[] { "鼠", "牛", "虎", "兔" };
int index = GetTigerIndex(data);
data[index] = "老虎";
Display(data);
Console.ReadLine();
}
private static int GetTigerIndex(string[] data)
{
return Array.IndexOf(data, "虎");
}
44. 謝謝各位
• 本投影片所包含的商標與文字皆屬原著作者所有。
• 本投影片使用的圖片皆從網路搜尋。
• 本著作係採用姓名標示-非商業性-相同方式分享 3.0 台灣授權。閱讀本授權條款,請到
http://creativecommons.org/licenses/by-nc-sa/3.0/tw/,或寫信至Creative Commons, 444 Castro
Street, Suite 900, Mountain View, California, 94041, USA.
h t t p s : / / m v c . t w