[C#]6.0和7.0後的cheat sheet

  • 1124
  • 0
  • C#
  • 2018-04-25

[C#]6.0和7.0後的cheat sheet

前言

這篇純綷只是紀錄一下我個人認為比較會常用到的部份,有些我已經很常用在專案上了,有些是覺得很好用,但忘了還可以這樣用的語法,而C# 7目前似乎對低於2017的版本支援性還沒那麼好,所以並還沒用在正在開式上,然後有些語法可能是我不知道要在什麼情境下使用,畢竟這篇是為了供我自己日後好參考,大部份不會有什麼特別說明,可能就只是簡單的cheat sheet,方便我之後可以快速查詢。

導覽

  1. Auto-property enhancements(自動實做屬性 c# 6)
  2. Expression bodies on property-like function members(用lambda的方式實做只取得屬性 c# 6)

  3. Using static( c# 6)

  4. Null-conditional operators(簡單判斷物件是否為空 c# 6)

  5. String interpolation(用template的方式來組字串 c# 6)

  6. is-expression(判斷object型別 c# 7)

  7. 強型別的truple(c# 7)

  8. Local Function(可以在Method內部定義Method c# 7)

  9. Binary literals and digit separators(可以為數字加上分隔符號 c# 7)

  10. Pattern matching - switch(switch判斷型別)

  11. Discard變數(可忽略回傳的Truple)

 

Auto-property enhancements(自動實做屬性 6.0)

void Main()
{
	Employee.Employees.Dump();
}


public class Employee
{
	public int Id { get; set; }
	public string Name { get; set; }

	public static IList<Employee> Employees { get; set; } = new List<Employee>
	{
		new Employee{Id=1,Name="kin"},
		new Employee{Id=1,Name="anson"},
		new Employee{Id=1,Name="kin1"},
	};
}

Expression bodies on property-like function members(用lambda的方式實做只取得屬性 6.0)

這樣子指定屬性的方式,是唯讀的。
 

void Main()
{
	Employee.Employees.Dump();
}


public class Employee
{
	public int Id { get; set; }
	public string Name { get; set; }

	public static IList<Employee> Employees => new List<Employee>
	{
		new Employee{Id=1,Name="kin"},
		new Employee{Id=2,Name="anson"},
		new Employee{Id=3,Name="kin1"},
	};
}

Using static(6.0)

using static System.Console;

void Main()
{
	WriteLine("dfadfds");
}

Null-conditional operators(簡單判斷物件是否為空)

void Main()
{
	if (Employee.Employees.FirstOrDefault()!=null)
	{
		Employee.Employees.FirstOrDefault().Name.Dump();
	}
	
	//用?.就代表了上面那三行的意思
	Employee.Employees.FirstOrDefault()?.Name.Dump();
}


public class Employee
{
	public int Id { get; set; }
	public string Name { get; set; }

	public static IList<Employee> Employees => new List<Employee>();
}

String interpolation(用template的方式來組字串)

void Main()
{
	int old = 26;
	string myOld = $"I am {old} years old";
	howOldAreYou.Dump();
}

Pattern matching is-expressions(判斷object型別並指定變數 c# 7)

object foo = "Hello, world";

if (foo is string s)
{
	s.Length.Dump(); //s在此就可以取得intellisense了
}

強型別的truple(c# 7)

這該算是C#7最亮眼的功能,可以不用另定義物件,也可以享有良好命名並回傳多個屬性了(你必須從nuget新增System.ValueTuple)

void Main()
{
        var (name,time)=NamedFoo();//第一種寫法

	NamedFoo().name.Dump();//第二種寫法
	NamedFoo().time.Dump();
}

static (string name, DateTime time) NamedFoo()
{
	return  ("Now", DateTime.Now);
}

Local Function(可以在Method內部定義Method c# 7)

void Main()
{
	Fibonacci (10).Dump();
}

public string Fibonacci (int x)
{
	return GetMessage();
	
	string GetMessage()
	{
		return "Hello World";
	}
}

Binary literals and digit separators(可以為數字加上分隔符號 c# 7)

void Main()
{
	int i=1_000;
	i.Dump();
}

Pattern matching - switch(switch判斷型別)

void Main()
{
	Demo ("s");
	Demo (1);
	Demo (3);
	Demo (DateTime.Today);
}

void Demo(object type)
{
	switch (type)
	{
		case string s:              // We can switch on *type*
			"is string".Dump();
			break;
		case int i when (i == 1 || i==2):
			"i is int".Dump();
			break;
		case DateTime d:
			"d is datetime".Dump();
			break;
	}
}

當然可以用switch的話,就可以使用if判斷式囉

void Main()
{
	Demo ("s");
	Demo (1);
	Demo (3);
	Demo (DateTime.Today);
}

void Demo(object type)
{
	if (type is int)
	{
		"is int".Dump();
	}
	
}

但是注意一下,雖然我們判斷出變數了,不過還是有型別上的問題,此時我們可以直接在後面定義一個變數,直接做加總

void Main()
{
	Demo(1).Dump();
	Demo(3).Dump();
}

int Demo(object type)
{
	int i=1;
	if (type is int val)
	{
		return i+val;
	}
	return 0;
}

Discard變數(可忽略回傳的Truple)

當我們用tuple回傳多個值的時候,如果我們有些變數使用不到,可以使用_來接,就省略了定義此變數,也不會造成程式碼閱讀者的困擾。

void Main()
{
	var (_name,time,_,_)=NamedFoo();
}

static (string name, DateTime time,int number1,int number2) NamedFoo()
{
	return ("Now", DateTime.Now,1,2);
}

結論

這篇是筆者個人簡單的紀錄,以後有新的語法也會直接在這篇補上去,當然如果有人覺得有什麼很好的特性,但我還沒發現的話,再請提醒告知筆者一下。