C 學習 關於this的幾種用法

2021-08-17 00:12:36 字數 1217 閱讀 1302

c#中,this 關鍵字的幾種用法

1. 用this來區分引數和成員名

public class person

;public string name

public *** ***

public person(string name, *** ***)

}

2. 用this來將乙個object作為引數傳遞

public class person2

;public string name

public *** ***

public person2(string name, *** ***)

public void showname()

}

3. 用this來宣告indexer

public class person3

;public string name

public *** ***

public person3(string name, *** ***)

public void showname()

public stringthis[string key]

set}

}

person3 p = new person3("tom", person3.***.male);

p["tom"] = "jerry";

p["jerry"] = "tom";

4. 用this來定義類擴充套件函式

public inte***ce ifoo

}public static class fooextentions

public class mytype : ifoo

}...

ifoo l3_a = new mytype ;

l3_a.nextmarker();

5. 用this來呼叫不同簽名的建構函式

public class foo

foo(int x) : this(x, 10) {} // y defaults to 10

}

using的幾種用法 C

using system using 引用命名空間 using system.collections.generic using system.text using system.io using system.data using system.data.sqlclient using syste...

c 中「?」的幾種用法

c 中 的幾種用法 1 可空型別修飾符 如 a b表示如果a為null則返回b,否則返回a 2 三元運算子 如 bool f false return f true?1 0 如果f為true則返回1,否則返回0 3 空合併運算子 如 a?b 當a為null時則返回b,a不為null時則返回a本身 a...

C 中 new的幾種用法

今天看到乙個朋友問到c 的new關鍵字有幾種用法,現將從網上摘抄彙總資料到本站,以供大家查閱 1 new運算子 用於建立物件和呼叫建構函式。2 new修飾符 用於隱藏基類成員的繼承成員。3 new約束 用於在泛型宣告中約束可能用作型別引數的引數的型別。new運算子 1.用於建立物件和呼叫建構函式 例...