C#与C++的几个不同之处知识点
1.索引器
索引器是C#自創(chuàng)的內(nèi)容,這是C++當(dāng)中沒有的內(nèi)容,所以做一次筆記.
索引器是用于書寫一個可以通過使用[]想數(shù)組一樣直接訪問集合元素的方法。我們只需要指定待訪問實例或元素的索引。索引器的語法和類屬性語法相同,除了接受作為元素索引的輸入?yún)?shù)外。
class SampleCollection<T> {// 聲明一個數(shù)組來存儲數(shù)據(jù)元素。private T[] arr = new T[100];// 定義索引器,它允許客戶機代碼 / /類實例上使用[]符號本身。 / /下面的代碼主要(參見第2行。) public T this[int i]{get{//這個索引器非常簡單,僅僅返回或設(shè)置 //數(shù)組從內(nèi)部相應(yīng)的元素。return arr[i];}set{arr[i] = value;}} }//program類顯示了客戶端代碼使用索引器。 class Program {static void Main(string[] args){// 聲明SampleCollection類型的一個實例。SampleCollection<string> stringCollection = new SampleCollection<string>();// 使用[]符號的類型。stringCollection[0] = "Hello, World";System.Console.WriteLine(stringCollection[0]);} }?
?2.拆箱與裝箱
裝箱:所謂裝箱是將一個原始類型或基礎(chǔ)類型對象賦給一個object對象,object是C#基類,所有類都繼承自object類
拆箱:反之,拆箱是將一個事先裝箱過的object對象強制轉(zhuǎn)換為目標(biāo)對象。
例如:
class Test {static void Main(){int mInt;object obj=mInt;//裝箱int myInt=(int) obj;//拆箱 } }3.參數(shù)的按值傳遞與按引用傳遞
在C#當(dāng)中,按值傳遞與C++是一樣的,這里不做介紹了;主要說一下按引用傳遞。
C#使用關(guān)鍵字ref指定引用參數(shù),在形參中使用ref的同時,在實參中也要用ref,否則將出現(xiàn)編譯錯誤,另外你不能將一個沒用初始化的對象按引用傳遞給函數(shù)。
例如:
class TestRef {int a=5;myMethod(ref a);//use func by ref .........void myMethod(ref int a){//function block } }4.輸出參數(shù)
輸出參數(shù)只是從函數(shù)返回值的參數(shù),輸入值不要求。C#使用關(guān)鍵詞out表示輸出參數(shù)。
例如:
class TestOut {int val;getNodeValue(val);..........bool getNodeValue(out int val){val = value;return true;}5.可變數(shù)量的參數(shù)和數(shù)組
C#中的數(shù)組使用關(guān)鍵詞param進行傳遞,一個數(shù)組類型的參數(shù)必須總是函數(shù)最右邊的參數(shù).可以傳遞任意數(shù)量的的元素作為數(shù)組類型的參數(shù)。
例如:
class TestArray{void func(param int []array){print(array.length);}func(1);//輸出1 func();//輸出0 func(7,9,4);//輸出3 func(new int[]array {4,6,7,3,5});//輸出5int []array2=new int[]{4,5,6,1,2,3};func(array2);//輸出6 }6.運算符與表達式
?is關(guān)鍵字:
is關(guān)鍵字的作用是檢查目標(biāo)對象與給定對象是否兼容;如果所提供的表達式非空,并且所提供的對象可以強制轉(zhuǎn)換為所提供的類型而不會導(dǎo)致引發(fā)異常,則?is?表達式的計算結(jié)果將是?true。
?例如:
class test1{} class test2{} class test3: test2{}class isTest {test1 t1;test2 t2;static void Test(object obj){if(obj is t1){print("obj is test1 classes");t1 = (test1)obj;}else if(obj is t2){print("obj is test2 classes);t2 = (test2)obj;}else{print("obj not is test1 and test2");} }static void Main(){test1 T1=new test1();test2 T2 = new test2();test3 T3 = new test3();test(T1);test(T2);test(T3);test("my test content");} }as關(guān)鍵字:
?as關(guān)鍵字類似于C++強制類型轉(zhuǎn)換,但是如果強制轉(zhuǎn)換不成功,as將返回null而不引發(fā)異常。
例如:
namespace MyFirstApp {class Base{public override string ToString(){return "base";}}class Device : Base{public delegate void GetVisiter(string name);}class Program{ static int Main(){Device de = new Device();Base b = de as Base;if(de != null){System.Console.WriteLine(de.ToString());}return 0;}} }表達式:
expression as type和下面的代碼是一樣的效果,說明as關(guān)鍵字是通過is來實現(xiàn)的
expression is type ? (type)expression : (type)null?sealed 關(guān)鍵詞
被sealed修飾的類不能被實例化
7.接口類interface
在C#當(dāng)中不引許多繼承,多繼承應(yīng)該考慮把基類聲明為interface,并且在interface聲明封裝的類中,不引許函數(shù)的實現(xiàn),只能寫出聲明,就像C++的頭文件,函數(shù)的實現(xiàn)留給派生類或結(jié)構(gòu)去實現(xiàn)。
例如:
//interface interface Equatable<T> {bool Equals(T obj);//只能寫出函數(shù)聲明 }// public class TestEquals : Equatable<TestEquatable> {public string Make { get; set; }public string Module { get; set; }public string Year { get; set; }public bool Equals(TestEquals te)//實現(xiàn)基類當(dāng)中的Equals函數(shù) {if(this.Make==te.Make &&this.Module==te.Module &&this.Year==te.Year){return true;}else{return flase} }注意:
接口具有下列屬性:
-
接口與抽象基類。?實現(xiàn)接口的任何類或結(jié)構(gòu)必須實現(xiàn)其所有成員的鏈接。
-
接口不能直接實例化。?其成員通過實現(xiàn)接口的任何類或結(jié)構(gòu)實現(xiàn)。
-
接口可以包含事件、索引器、方法和屬性。
-
接口不包含方法的實現(xiàn)。
-
類或結(jié)構(gòu)可以實現(xiàn)多個接口。?類可以繼承基類并實現(xiàn)一個或多個接口
轉(zhuǎn)載于:https://www.cnblogs.com/Leekin/p/5813087.html
與50位技術(shù)專家面對面20年技術(shù)見證,附贈技術(shù)全景圖總結(jié)
以上是生活随笔為你收集整理的C#与C++的几个不同之处知识点的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: [ An Ac a Day ^_^ ]
- 下一篇: RANSANC算法