學習C#的一些筆記

接口與類的區別:

1. 不允許使用訪問修飾符(public, private, protected或internal),所有接口都必須是公共的。

2. 接口成員不能包含代碼體。

3. 接口不能定義字段成員。

4. 接口成員不能使用關鍵字static, virtual, abstract或sealed(封閉的)來定義。

5. 類型定義成員是禁止的。這點不明白

 

隱藏基類成員,用關鍵字

new來定義它們(顯示隱藏)

override重寫父類中的方法

 

virtual用來定義虛方法,用來被重寫。

 

調用重寫或隱藏基類方法:

base.parentMethod();

this.childMethod();

 

嵌套類型定義,調用:

MyClass.myNestedClass myObj = new MyClass.myNestedClass();

 

屬性定義:

public int MyInt {get; set;}

 

partial關鍵字,將一個類分開-成幾個文件

A.cs文件

partial class MyClass

{ public void RtuStrin() {}}

B.cs文件

partial class MyClass

{ public int MyInt; }

C.cs文件

MyClass myClass = new MyClass();

myClass.MyInt = 1;

myClass.RtuStrin();

 

集合:

ArrayList--容量不是固定的

Animal[] animalArray = new Animal[2];// 定義了固定長度的數組

ArrayList animalArrayList = new ArrayList(); // 未定義固定長度的數組

 

foreach是IEnumerable接口所支持的

AddRange:向ArrayList中插入一個ArrayList

InsertRange():向ArrayList中插入一個ArrayList指定位置

IList()---->Clear(), RemoveAt();

ICollection---->Count;

Dictionary---->Remove();

 

Boxing & Unboxing, Nullable, ref, out, param

 

Delegate Example:

public delegate void DelegateMethod(int number);

public class Test {public DelegateMthod dm; }

public class Test1 { public void Method1 { Console.WriteLine("Test1"); }}

public class Test2 { public void Method2 { Console.WriteLine("Test2"); }}

public class App {

    public static void main() {

        Test t = new Test();

        Test1 t1 = new Test1();

        Test2 t2 = new Test2();

        t.dm += new DelegateMethod(t1.Method1);

        t.dm += new DelegateMethod(t2.Method2);

        t.dm(5);

    }

}

 

Lock: 線程同步安全,可以鎖住某個對象

 

StringBuilder:

 

使用繼承顯示實現接口成員:在方法前面加上接口名稱(實現接口某個方法:public interfaceName.methodName())

 

C#裏面的成員可以包括:

字段,屬性,方法,事件,運算符,索引器,構造函數,析構函數,嵌套函數

 

 

C#中常用的異常類型:

System.Exception;

System.NullReferenceException;

System.ArgumentOutOfRangeException;

System.InvalidCaseException;

發佈了26 篇原創文章 · 獲贊 0 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章