C 4 0新特性之元組Tuple

2021-07-22 07:02:25 字數 1603 閱讀 6289

組元使用泛型來簡化乙個類的定義。

public class point

public int y

}//the user customer data type.

point p = new point() ;

//use the predefine generic tuple type.

tuplep2 = new tuple(10, 20);

//console.writeline(p.x + p.y);

console.writeline(p2.item1 + p2.item2);

乙個簡單的包含兩個int型別成員的類,傳統的方法定義point需要寫很多**,但是使用tuple卻只有一句,組元多用於方法的返回值。如果乙個函式返回多個型別,這樣就不在用out , ref等輸出引數了,可以直接定義乙個tuple型別就可以了。非常方便。

//1 member

tupletest = new tuple(1);

//2 member ( 1< n <8 )

tupletest2 = tuple.create(1,2);

//8 member , the last member must be tuple type.

tuple> test3 = new tuple>(1, 2, 3, 4, 5, 6, 7, new tuple(8));

//console.writeline(test.item1);

console.writeline(test2.item1 + test2.item2);

console.writeline(test3.item1 + test3.item2 + test3.item3 + test3.item4 + test3.item5 + test3.item6 + test3.item7 + test3.rest.item1);

第乙個定義包含乙個成員。

第二個定義包含兩個成員,並且使用create方法初始化。

第三個定義展示了tuple最多支援8個成員,如果多於8個就需要進行巢狀。注意第8個成員很特殊,如果有8個成員,第8個必須巢狀定義tuple。如果上面所示。

//2 member ,the second type is the nest type tuple.

tuple> test4 = new tuple>(1, new tuple(2));

//10 member datatype. nest the 8 parameter type.

tuple> test5 = new tuple>(1, 2, 3, 4, 5, 6, 7, new tuple(8, 9, 10));

//console.writeline(test4.item1 + test4.item2.item1);

console.writeline(test5.item1 + test5.item2 + test5.item3 + test5.item4 + test5.item5 + test5.item6 + test5.item7 + test5.rest.item1 + test5.rest.item2 + test5.rest.item3);

C 4 0 新特性之引數

c 4.0中新加了幾種引數,今天來了解一下 1.預設引數 c 4.0 現在對方法,構造器,以及索引器支援使用可預設的引數 當引數的預設值作為宣告的一部分被指定的時候,引數就是可預設的。static void changevalue intx,inty 3 int y 有乙個預設的引數3,因此它是乙個...

C 4 0 新特性之引數

c 4.0中新加了幾種引數,今天來了解一下 1.預設引數 c 4.0 現在對方法,構造器,以及索引器支援使用可預設的引數 當引數的預設值作為宣告的一部分被指定的時候,引數就是可預設的。static void changevalue intx,inty 3 int y 有乙個預設的引數3,因此它是乙個...

C 新特性元組 tuple

python程式設計師一定對元組這個詞非常熟悉,python中的元組很強大,剛接觸python時我一直認為python中的元組是對c 陣列的封裝,吸收了c 的陣列的精華並進行了擴充套件。c 11以後直接增加了元組這個資料結構,或許是c 標準制定者看到了python中元組的方便,索性也直接吸收了吧。自...