您好,登錄后才能下訂單哦!
小編給大家分享一下C#3.0中如何實(shí)現(xiàn)自動(dòng)屬性和對(duì)象初始化器,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
1.匿名屬性
定義屬性如下:
public class Employee { public int Id { get; set; } public string Name { get; set; } public string Sex { get; set; } public int Age { get; set; } public string BirthDate { get; set; } } 在C#3.0 之前的寫法如下: public class Employee { private int _id; private string _name; private string _sex; private int _age; private string _birthDate; public int Id { get { return _id; } set { _id = value; } } public string Name { get { return _name; } set { _name = value; } } public string Sex { get { return _sex; } set { _sex = value; } } public int Age { get { return _age; } set { _age = value; } } public string BirthDate { get { return _birthDate; } set { _birthDate = value; } } }
僅從代碼量上就前者比后者減少了2/3的代碼,這對(duì)提高效率是顯而易見的,那么這些代碼都到哪里去了呢?其實(shí)那些額外的代碼都是由編譯器為我們完成的,編譯器會(huì)將那些我們“省去"的代碼編譯成托管IL代碼的時(shí)候補(bǔ)進(jìn)去,兩者中代碼在最終生成的IL代碼的體積是差不多的。
上圖中的IL代碼中我們看到了
2.對(duì)象初始化器,原來的對(duì)象初始化都要進(jìn)行先創(chuàng)建構(gòu)造方法,然后才能進(jìn)行成員的相關(guān)操作,C#3.0 提供了對(duì)象成員的直接初始化的能力,和初始化一個(gè)集合或者是數(shù)組一樣來初始化對(duì)象。
相面來看,通過對(duì)象初始化器對(duì)上面的的Employee類進(jìn)行進(jìn)行調(diào)用:
Employee employee = new Employee { Id = 1, Name = "藍(lán)之風(fēng)", Age = 24, BirthDate = "1984-10-21", Sex = "男" }; BirthDate = "1984-10-21", Sex = "男" }; |
就是對(duì)象的初始化,看到代碼很簡潔,輸出的結(jié)果如下:
在C#3.0之前的做法是:
Employee employee = new Employee(); |
或者通過重載構(gòu)造方法的方式來初始化這些屬性,二者的達(dá)到的效果是相同的,但是前者使用起來方便了些,代碼量減少了許多,這個(gè)過程是怎么完成的呢?其實(shí)C#本身并沒有太大的變化,這些都是在語法上的一些改變,使得編寫代碼的時(shí)候更方便效率更高,把一些編譯器可以推斷出來完成的工作讓編譯器來做了,編譯器在編譯程序的時(shí)候?qū)⑽覀儧]有實(shí)現(xiàn)的代碼替我們實(shí)現(xiàn)來生成IL代碼的:
method private hidebysig static void Main(string[] args) cil managed
{
.entrypoint
// 代碼大小 175 (0xaf)
.maxstack 2
.locals init ([0] class CS30.Employee employee,
[1] class CS30.Employee '<>g__initLocal0')
IL_0000: nop
IL_0001: newobj instance void CS30.Employee::.ctor()
IL_0006: stloc.1
IL_0007: ldloc.1
IL_0008: ldc.i4.1
IL_0009: callvirt instance void CS30.Employee::set_Id(int32)
IL_000e: nop
IL_000f: ldloc.1
IL_0010: ldstr bytearray (DD 84 4B 4E CE 98 ) // ..KN..
IL_0015: callvirt instance void CS30.Employee::set_Name(string)
IL_001a: nop
IL_001b: ldloc.1
IL_001c: ldc.i4.s 24
IL_001e: callvirt instance void CS30.Employee::set_Age(int32)
IL_0023: nop
IL_0024: ldloc.1
IL_0025: ldstr "1984-10-21"
IL_002a: callvirt instance void CS30.Employee::set_BirthDate(string)
IL_002f: nop
IL_0030: ldloc.1
IL_0031: ldstr bytearray (37 75 ) // 7u
IL_0036: callvirt instance void CS30.Employee::set_Sex(string)
IL_003b: nop
IL_003c: ldloc.1
IL_003d: stloc.0
IL_003e: ldstr bytearray (16 7F F7 53 3B 00 7B 00 30 00 7D 00 ) // ...S;.{.0.}.
IL_0043: ldloc.0
IL_0044: callvirt instance int32 CS30.Employee::get_Id()
IL_0049: box [mscorlib]System.Int32
IL_004e: call void [mscorlib]System.Console::WriteLine(string,
object)
IL_0053: nop
IL_0054: ldstr bytearray (D3 59 0D 54 3A 00 7B 00 30 00 7D 00 ) // .Y.T:.{.0.}.
IL_0059: ldloc.0
IL_005a: callvirt instance string CS30.Employee::get_Name()
IL_005f: call void [mscorlib]System.Console::WriteLine(string,
object)
IL_0064: nop
IL_0065: ldstr bytearray (74 5E 84 9F 3A 00 7B 00 30 00 7D 00 ) // t^..:.{.0.}.
IL_006a: ldloc.0
IL_006b: callvirt instance int32 CS30.Employee::get_Age()
IL_0070: box [mscorlib]System.Int32
IL_0075: call void [mscorlib]System.Console::WriteLine(string,
object)
IL_007a: nop
IL_007b: ldstr bytearray (1F 75 E5 65 3A 00 7B 00 30 00 7D 00 ) // .u.e:.{.0.}.
IL_0080: ldloc.0
IL_0081: callvirt instance string CS30.Employee::get_BirthDate()
IL_0086: call void [mscorlib]System.Console::WriteLine(string,
object)
IL_008b: nop
IL_008c: ldstr bytearray (27 60 2B 52 3A 00 7B 00 30 00 7D 00 ) // '`+R:.{.0.}.
IL_0091: ldloc.0
IL_0092: callvirt instance string CS30.Employee::get_Sex()
IL_0097: call void [mscorlib]System.Console::WriteLine(string,
object)
IL_009c: nop
IL_009d: ldstr bytearray (F7 8B 09 63 FB 4E 0F 61 2E 95 E7 7E ED 7E 2E 00 // ...c.N.a...~.~..
2E 00 2E 00 ) // ....
IL_00a2: call void [mscorlib]System.Console::WriteLine(string)
IL_00a7: nop
IL_00a8: call string [mscorlib]System.Console::ReadLine()
IL_00ad: pop
IL_00ae: ret
} // end of method Program::Main
以上是“C#3.0中如何實(shí)現(xiàn)自動(dòng)屬性和對(duì)象初始化器”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。