C 7 0 特性例項

2021-08-31 11:55:23 字數 3026 閱讀 2049

using system;

using system.collections.generic;

using system.linq;

using system.text;

using system.threading.tasks;

namespace csharp_7._0

,", a, b);

function_ref(ref a,ref b);

console.writeline(",",a,b);

function_out(out int c);

console.writeline(c);

//2.元組()

//在4.0中有返回多個值的解決方案

var data = tuple();

//var是乙個語法糖,在宣告變數的時候就已經決定了是什麼型別

//dynamic 並不是在編譯的時候確定型別,而是在執行的時候,所以有時候會編譯通過,然後執行報錯

console.writeline(data.item1);

console.writeline(data.item2);

//然而在7.0中提供了乙個更好的方案()需要引用system.valuetuple

var data1 = tuple1();

console.writeline(data1.a);

元組還能這麼用

var pink = ("a","b");

console.writeline(pink.item1);

(string xiao, string shi) aha = ("woshi","xiaoshi");

console.writeline(aha.xiao);

棄元//通常,在進行元組解構或使用 out 引數呼叫方法時,必須定義乙個其值無關緊要且你不打算使用的變數。 

//為處理此情況,c# 增添了對棄元的支援。 棄元是乙個名為 _(下劃線字元)的只寫變數,可向單個變數賦予要放棄的所有值。

// 棄元類似於未賦值的變數;不可在**中使用棄元(賦值語句除外)。

var (_, _, _, pop1, _, pop2) = querycitydataforyears("new york city", 1960, 2010);

console.writeline($"population change, 1960 to 2010: ");

模式匹配

object x = 1;

if (a is int y) //這裡,判斷為int後就直接賦值給y

//switch新玩法

add("d");

console.writeline(add(-1));

區域性變數和引用返回

int x1 = 3;

ref int x2 = ref x1;  //注意這裡,我們通過ref關鍵字 把x賦給了x1

x2 = 2;

console.writeline($"改變後的變數 值為: ");//$是6.0的特性。相當於string.format()

//引用返回允許我們可以把值型別當作引用型別來進行return

int arr = ;

ref int x3 = ref getbyindex(arr, 2); //呼叫剛才的方法

x3 = 99;

console.writeline($"陣列arr[2]的值為: ");

int y1 = 1, y2 = 2, y3 = 3;

ref int max_= ref max(ref y1, ref y2, ref y3);

console.writeline($"最大值:");

max(ref y1, ref y2, ref y3) = 4;

console.writeline($"最大值:");

區域性函式

//只在特定的函式中可以訪問的函式

void dosomeing()

} more expression-bodied members

test test = new test("s");

異常表示式

數字文字化

int bxh = 0xab_cd_ef;//使數字更加易讀

console.read();

}static void function_ref(ref int a,ref int b)//必須是a和b,不能是別的

static void no_function_ref(int a,int b)

static void function_out(out int c)

static tupletuple()

static (int a,int b,int c) tuple1()

private static (string, double, int, int, int, int) querycitydataforyears(string name, int year1, int year2)

if (year2 == 2010)

return (name, area, year1, population1, year2, population2);

}return ("", 0, 0, 0, 0, 0);

}static dynamic add(object a)

return data;

}static ref int getbyindex(int arr, int ix) => ref arr[ix];

static ref int max(ref int first,ref int second,ref int third)

public string isnull()

}class test

// expression-bodied finalizer

~test() => console.error.writeline("finalized!");

private string label;

// expression-bodied get / set accessors.

public string label}}

C 7 0 新特性4 返回引用

本文參考roslyn專案中的issue 118。1.c 7.0 新特性1 基於tuple的 多 返回值方法 2.c 7.0 新特性2 本地方法 3.c 7.0 新特性3 模式匹配 4.c 7.0 新特性4 返回引用 c 早在最初的發行版c 1.0中 2002年1月 就借鑑並延續了c c 中指標引數,...

C 7 0新特性和語法糖詳解

伴隨visual studio 2017的發布,c 7.0開始正式走上工作崗位。對於早已熟悉了舊版本c 的開發者來說,c 7.0增加的不少新特性和語法糖能在很大程度上提公升程式設計效率並降低出錯率。本文將闡述c 7.0給出的9個改進。1 元組 更優雅地返回多個值 之所以將元組放在第一位,是因為它對c...

C 7 0中的解構功能 Deconstruct

c 7.0新增了諸多功能,其中有一項是新元組 valuetuple 它允許我們可以返回多個值,並且配合解構能更加方便的進行工作,如下面例子 static void main string args nage public static string name,int age getuser 可以看到...