015.C#基礎總結

來源:互聯網
上載者:User

標籤:sub   winform   files   foreach   static   調用   return   arraylist   命名空間   

一、語言基礎
1、VS .net C#
Console類:
WriteLine() Write()
ReadLine()
2、變數、資料類型、運算子
資料類型 變數名; 變數名 = 值;

int float double char bool string
byte sbyte

++ --
?:

const:常量

3、流程式控制制語句
選擇語句
if..else

switch...case

迴圈語句
初始化 迴圈條件 迴圈操作 更新

for
while
do..while
do
{
}while(條件);

跳躍陳述式
break
continue
goto

迴圈嵌套

4、方法
存取修飾詞 [可選修飾符] 傳回型別 方法名(參數列表)
{
方法體
}
public
protected
internal
private

static
abstract
virtual
override
sealed

void
類型

參數列表:資料類型 參數名1,資料類型 參數2,....

直接調用:方法名(參數);
類名調用:類名.方法(參數);
對象調用:對象.方法(參數);

ref:引用參數
out:輸出參數

bool int.TryParse(string,out int)

方法的重載


5、數組
一位元組
int[] arr;
arr=new int[5];
arr[0]=1;

int[] arr=new int[5];

int[] arr=new int[]{1,2,3,4,5};
int[] arr;
arr=new int[]{1,2,3,4,5};

int[] arr={1,2,3,4,5};
int[] arr;
arr={1,2,3,4,5};//錯誤

常用演算法:
累加求和
求平均
求最值

冒泡排序

數組的常用屬性和方法
Length
Rank

Sort()
IndexOf()


二維數組:
int[,] arr=new int[3,4]{{1,2,3,4},{2,3,4,5},{3,4,5,6}};
交錯數組:
int[][] arr=new int[3][];
arr[0]=new int[5];

foreach(元素類型 變數名 in 數組或集合)
{
//不能改變元素的值
}

參數數組

6、string StringBuilder
string的常用方法

Split()
Substring()
IndexOf()
LastIndexOf()
Insert()
Replace()

Join()
Format()

string的不可變性

StringBuilder
ApplendLine()
AppendFormat()

枚舉
public enum 枚舉
{

}
結構
public struct 結構
{

}


二、OOP
1、類和對象
類是對象的資料類型
類是抽象的 對象是具體的

建構函式:

this:當前執行個體
static
partial:部分類別

建立對象
類名 引用 = new 建構函式();

2、類繼承
父類 子類
基類 衍生類別
public class 子類:父類
{}

特性:單一繼承 傳遞性

base

子類的建構函式預設調用父類的無參建構函式,可以使用base 顯示調用父類的建構函式
public 子類建構函式(參數列表):base(實參)
{

}

抽象方法、抽象類別
abstract
抽象方法:沒有實現 只能在抽象類別中定義 必須在非抽象子類中實現
抽象類別:不能執行個體化 通常被繼承 可以包含實現方法、虛方法、抽象方法

方法的重寫:override
方法的簽名和父類中相同

虛方法:有實現 除靜態類、密封類之外的類中定義 可以在子類中重寫


裝箱拆箱:
裝箱:實值型別向object或其實現的介面的隱式轉換
拆箱:object向實值型別或介面向實現該介面的實值型別的顯示轉換

隱藏方法:new


3、多態
父類類型指向子類對象時,重寫方法 調用的是子類重寫之後的 隱藏方法 調用的是父類的

密封方法 密封類
sealed
密封方法:不能被重寫 sealed override
密封類:不允許被繼承 不能包含抽象方法和虛方法

4、介面:行為的規範
interface
不能有實現
不能有欄位
所有成員預設public abstract

實作類別必須實現介面中的全部成員 添加public 不能使用override
如果不能實現,聲明為抽象方法 類聲明為抽象類別

介面可以實現多繼承

public class MyClass:父類,介面1,介面2,...
{

}

IComparable 方法: int CompareTo(object )

is:對象 is 類 子類相容父類
as:強制類型轉換 對象 as 類型 成功:對象 失敗:null

命名空間:


5、屬性 索引器
get、set訪問器

public 類型 屬性名稱
{
get
{
return this.欄位;
}
set
{
this.欄位 = value;
}
}

自動屬性:讀寫屬性


public 類型 this[資料類型 索引]
{
get
{
return this.數組[索引];
}
set
{
this.數組[索引]=value;
}
}
索引可以是任意資料類型

6、委託、事件
委託是一種引用資料類型,在命名空間中定義

public delegate 傳回型別 委託(參數列表);

委託 dele=new 委託(方法名);
dele(參數);
dele+=方法名;

事件:定義在類中
public event 委託 事件名;

//註冊
事件名+=方法名;

三、Framework
1、集合
System.Collections
ArrayList:動態數組
Hashtable:索引值對
Stack:棧 後進先出
Queue:隊列 先進先出
SoredList:按鍵排序

2、泛型
泛型類
泛型方法
泛型結構:Nullable<T>

預設值
T t = default(T);
約束
struct
class
base class
interface
new()

where T : 約束

泛型集合
List<T>
Dictionary<K,V>

WinForm
控制項
事件

檔案和流
System.IO
File FileInfo
Directory DirectoryInfo
DriveInfo

Stream
FileStream
StreamWriter
StreamReader


序列化
對象持久化


調試


異常處理
執行階段錯誤
Exception及其子類

try
{

}
catch()
{

}
finally
{

}


記憶體管理


多線程


程式集
.exe .dll
程式集的引用


反射
System;
System.Reflection;

System.Type
System.Reflection.Assembly;


設定檔:App.config

<AppSettings>
<Add key="" value=""/>
</AppSettings>

 

015.C#基礎總結

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.