C#快速入門指南

C#

集成開發環境

Visual Studio程序員

結構

using System;

包含 System 命名空間正則表達式

class hello{ /*註釋*/
static void Main(string\[\] args){
//一個文件一個main函數
}
}

結構體

struct Books{變量...}

結構不支持繼承。
結構不能聲明默認的構造函數
express

枚舉

enum Day { Sun, Mon, tue, Wed, thu, Fri, Sat };

Day.Sun==0數組

Day.Mon==1安全

接口

public interface PaintCost {int getCost(int area);}

派生類

class Rectangle : hello, PaintCost{}

全析

namespace first{

命名空間聲明函數

 public class Test{

能夠多個class
構造函數、析構函數、靜態成員...
oop

變量...測試

		decimal a=10;

128 位精確的十進制值,28-29 有效位數ui

		sbyte b=2;

8 位有符號整數類型

		uint c;

32 位無符號整數類型

		ulong d;

64 位無符號整數類型

		ushort e;

16 位無符號整數類型

		dynamic f=a,g=b;

存儲任何類型的值在動態數據類型變量中

字符串

		"hhh\"hhh\nhhh"	==
		@"hhh"hhh\nhhh"	==
		@"hhh"hhh
			hhh"
		"hhh" + "hhh"

字符串一堆方法:拆分,複製...

可空類型,便可以賦值爲null

		int? a,b=null;	//a==null
		int a;		//a==0

合併運算符

		int b=a ?? 5;	//a爲null則b=5,不然b=a

多維數組

		int [,] a = new int [3,4] {
			{0, 1, 2, 3} ,	/*  初始化索引號爲 0 的行 */
			{4, 5, 6, 7} ,	/*  初始化索引號爲 1 的行 */
			{8, 9, 10, 11}	/*  初始化索引號爲 2 的行 */
			};

交錯數組

		int[][] scores = new int[5][];
		for (int i = 0; i < scores.Length; i++) 
			scores[i] = new int[4];

Array 類

		scores.Rank;	//獲取數組的秩(維度)
		scores.Length;
		Array.Copy(Array, Array, Int32);
		/*從數組的第一個元素開始複製某個範圍的元素
			到另外一個數組的第一個元素位置*/

其餘運算符

		&a	//地址
		*a	//指針
		obj is Car	// 檢查 obj 是不是 Car 類的一個對象。
		obj as int	//強制轉換,即便轉換失敗也不會拋出異常。

常量

		const

其餘常量例如8進制數

		0213

裝箱與拆箱

		object obj;
		obj = 100; 	//這是裝箱
		int a=obj as int;	//這是拆箱

循環

		foreach(變量 in 數組){}

方法...

		public int plus(int a){return a+1;}

重載

		public int plus(char a){return a+2;}

運算符重載

		public static Box operator+ (Box b, Box c){...}

可重載

  • +, -, !, ~, ++, -- ( op-type operand )

  • +, -, *, \/, %,==, !=, <, >, <=, >= ( op-type operand, op-type2 operand2 )

不可直接重載

  • &&, ||

不可重載

  • +=, -=, *=, /=, %=,=, ., ?:, ->, new, is, sizeof, typeof

按值傳遞

		(int a)	

引用

		(ref int a)

按輸出傳遞,即變量最後一個值賦回去

		(out int a)

傳遞數組給函數

		(int[] a)

參數數組

		int plus(params int[] arr)

傳遞時:

		plus(512, 720, 250, 567, 889)

訪問權限

public:全部對象均可以訪問;
internal:同一個程序集的對象能夠訪問;
protected internal:訪問限於當前程序集或派生自包含類的類型。

	}
}

命名空間聲明

namespace second{
	public class Test{
	}

嵌套命名空間

	namespace second{
	}
}

命名空間運用

class TestClass{
	void hhh(){
		first.Test;
		second.Test;
	}
}

using指令

using second;		//using指令:引入命名空間
using static System.Math;	//指定無需指定類型名稱便可訪問其靜態> 成員的類型
using Project = PC.MyCompany;		//起別名
using (Font font3 = new Font("Arial", 10.0f),	//將實例與代碼綁定
         font4 = new Font("Arial", 10.0f))

預處理器指令

#define PIQ	//定義一系列成爲符號的字符
#if(PIW)	{不會執行}	//測試符號是否爲真
#elif(PIQ)	{會執行}
#else	{}
#endif		//指定一個條件指令的結束
#undef PIQ	//取消定義符號

#line number filename	//修改編譯器的行數以及(可選地)輸出錯> 誤和警告的文件名
#line default		//恢復默認行號
#error 你錯了!		//從代碼的指定位置生成一個錯誤
#warning 你就錯了!	//從代碼的指定位置生成一級警告
#pragma warning disable 169    //取消編號 169 的警告(即字段> 未使用的警告)
#pragma warning restore 169   // 恢復編號 169 的警告

#region		//在使用 Visual Studio Code Editor 的大綱特性時,指定一個可展開或摺疊的代碼塊
#endregion	//標識着 #region 塊的結束

示例用法

#define  CONNECT(a,b)   a##b	//##粘連兩個標識符,只有宏定義中使用(#define)
int  CONNECT(a,1);			//int a1

傳統方式:

typedef struct _tag_Student Student;
struct _tag_Student{
	char* name;
	int id;
};

用宏定義方式

#define STRUCT(type)    typedef struct _tag_##type type;\
		       struct _tag_##type
STRUCT(Student){
	char* name;
	int id;
}; 

異常處理

try	{throw a;}		//a爲throwable
catch(a)	{}
finally	{}

在 catch 塊中使用 throw 語句來拋出當前的對象

class _throw{
void _throw(){
try{
	MethodThatThrowsException();
}
catch (Exception ex){
	//和根本不存在這個catch塊的時候同樣
	throw;
}
catch (Exception ex){
	//stack trace認爲你catch到的異常已經被處理了
	//只不過處理過程當中又拋出新的異常
	//這時候stack trace就把throw ex;看成錯誤根源了
	throw ex;
}
catch (Exception ex){	
	//stack trace會自動認爲內部異常是致使當前異常的緣由
	//也就會把內部異常的stack trace也遞歸顯示出來
	throw new Exception("oops!", ex);
}
}}

System.ApplicationException 類:

支持由應用程序生成的異常。因此程序員定義的異常都應派生自該類。

輸入與輸出

using System;
class _IO{
void _IO(){
	console.write("C#很迷");
	console.writeline("顯示的信息");
	Console.WriteLine("第一個學生的姓名{0},成績{1}",name1,score1);
		//「格式字符串」和變量列表,當有多個變量須要輸出時可使用該方法
		//{0}、{1}叫作佔位符,表明後面依次排列的變量表
	console.readkey();	//等待用戶按下任意鍵,一次讀入一個字符
	console.readline();	//string,當用戶按下enter鍵的時候一次讀取一行
	convert.toint32/todouble("32"); //convert用於不一樣類型數據之間的轉化
	int.Parse("32");	//這個也能轉化
}}

正則表達式:匹配專用,能夠用於輸入readline()的整理

Regex 類用於表示一個正則表達式

本身去看吧~

文件的輸入與輸出

特性,相似於Java的/**   */,生成說明註釋**

[attribute(positional_parameters, name_parameter = value, ...)]

class || method || 變量 ......
本身去看吧~

反射:

容許在運行時查看特性(attribute)信息。
容許審查集合中的各類類型,以及實例化這些類型。
容許延遲綁定的方法和屬性(property)。
容許在運行時建立新類型,而後使用這些類型執行一些任務。

不安全代碼

因爲C#中聲明的變量在內存中的存儲受垃圾回收器管理;所以一個變量(例如一個大數組)有可能在運行過程當中被移動到內存中的其餘位置。若是一個變量的內存地址會變化,那麼指針也就沒有意義了。

解決方法就是使用fixed關鍵字來固定變量位置不移動。

int[]  list = {10, 100, 200};
fixed(int *ptr = list)

或者在方法上標明unsafe

public unsafe static void Main(){
	int var = 20;
	int* p = &var;
}

或程序裏標明unsafe

unsafe{	int var = 20;
	int* p = &var;
}

注意,編譯時須要命令行編譯或者修改編譯器設置

相關文章
相關標籤/搜索