新聞中心
C#語言還是比較常見的東西,這里我們主要介紹C#匿名方法,包括介紹當C#匿名方法不需要帶參數(shù)的時候,后面的括號是可選的等方面。

烏海網(wǎng)站制作公司哪家好,找成都創(chuàng)新互聯(lián)!從網(wǎng)頁設(shè)計、網(wǎng)站建設(shè)、微信開發(fā)、APP開發(fā)、響應(yīng)式網(wǎng)站等網(wǎng)站項目制作,到程序開發(fā),運營維護。成都創(chuàng)新互聯(lián)從2013年開始到現(xiàn)在10年的時間,我們擁有了豐富的建站經(jīng)驗和運維經(jīng)驗,來保證我們的工作的順利進行。專注于網(wǎng)站建設(shè)就選成都創(chuàng)新互聯(lián)。
C#匿名方法
這是對變量范圍的擴展。但是,下面例子說明了匿名參數(shù)還能夠在它們的代碼塊之外執(zhí)行命名方法:
- privatedelegatevoidExample6();
- privateint _customerId;
- privatestring _customerCode;
- publicint CustomerID
- {
- get { return _customerId; }
- set { _customerId = value; }
- }
- publicstring CustomerCode
- {
- get { return _customerCode; }
- set { _customerCode = value; }
- }
- privatevoid btnExample6_Click(object sender, EventArgs e)
- {
- //Populate out properties.
- this.CustomerID = 90;
- this.CustomerCode = "1337HK";
- //Setup the delegate/anonymous method.
- Example6 example =
- newExample6(
- delegate
- {
- this.ShowCustomer(this.CustomerID, this.CustomerCode);
- });
- //Execute the delegate.
- example();
- //Change the properties.
- this.CustomerID = 54;
- this.CustomerCode = "L4M3";
- //Execute the delegate again.
- // Notice that the new values are reflected.
- example();
- }
- privatevoid ShowCustomer(int customerId, string customerCode)
- {
- MessageBox.Show(
- String.Format("CustomerID: Customer Code: ",
- customerId, customerCode));
- }
要注意的是,我兩次調(diào)用了與C#匿名方法相關(guān)聯(lián)的委托。你可能會發(fā)現(xiàn)一個很有趣的事情:在這些調(diào)用中,方法會輸出兩組不同的值。這是因為用在C#匿名方法里的外部變量在創(chuàng)建C#匿名方法的時候被引用。這意味著對這些變量的任何更改都會在匿名函數(shù)訪問變量的時候被反映出來。
你可能還注意到在這個實例里委托關(guān)鍵字后面沒有括號。當C#匿名方法不需要帶參數(shù)的時候,后面的括號是可選的。
網(wǎng)站標題:C#匿名方法詳細剖析
網(wǎng)頁URL:http://fisionsoft.com.cn/article/djeshjc.html


咨詢
建站咨詢
