新聞中心
C#語言有很多值得學(xué)習(xí)的地方,這里我們主要介紹C#多路廣播,包括介紹多路代理MSDN上翻譯成C#多路廣播等方面。

10余年的云縣網(wǎng)站建設(shè)經(jīng)驗,針對設(shè)計、前端、開發(fā)、售后、文案、推廣等六對一服務(wù),響應(yīng)快,48小時及時工作處理。全網(wǎng)整合營銷推廣的優(yōu)勢是能夠根據(jù)用戶設(shè)備顯示端的尺寸不同,自動調(diào)整云縣建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設(shè)計,從而大程度地提升瀏覽體驗。創(chuàng)新互聯(lián)建站從事“云縣網(wǎng)站設(shè)計”,“云縣網(wǎng)站推廣”以來,每個客戶項目都認(rèn)真落實執(zhí)行。
一個代理同時代理幾個方法。就是我們前面說到的那樣,你去建造房子,現(xiàn)在要不僅僅是建造住宅,還的去建造花園等等其它建筑物。可是它們都是在建造房子,傳遞的參數(shù)也相同返回值的類型也相同都是房屋。那么我們?yōu)槭裁床徽乙粋€代理人來完成這樣的任務(wù)呢?把這些事物交由他一個人來完成不是可以節(jié)省我們很多的時間和金錢。是的我們可以那樣做System。MulticastDelegate 實際上在.net framework中你還可以找到這個類,多路代理MSDN上翻譯成C#多路廣播。事實上它還重載了操作符+=。其實C#多路廣播和單路代理在使用方法上區(qū)別不大。你可以看下面的例子。
- using System;
- namespace Multi_castDelegate
- {
- ///
- /// Summary description for Class1.
- ///
- class MyClassDelegate
- {
- ///
- /// The main entry point for the application.
- ///
- public delegate string IntDelegate(string s);
- }
- }
- using System;
- namespace Multi_castDelegate
- {
- ///
- /// Summary description for MyImplementingClass.
- ///
- public class MyClass
- {
- public MyClass()
- {
- }
- public static string WriteString(string s)
- {
- Console.WriteLine("Writing string");
- return "null";
- }
- public static string logString(string s)
- {
- Console.WriteLine("loging string");
- return "null";
- }
- public static string TransmitString(string s)
- {
- Console.WriteLine("Transmitting string");
- return "null";
- }
- }
- }
- The Main class:
- using System;
- using System.Threading;
- namespace Multi_castDelegate
- {
- ///
- /// Summary description for Test.
- ///
- public class Test
- {
- public static void Main()
- {
- MyClassDelegate.StringDelegate
- Writer,Logger,Transmitter;
- MyClassDelegate.StringDelegate
- myDelegate;
- Writer=new
- MyClassDelegate.StringDelegate(MyClass.WriteString);
- /// calling Writer
- Writer("hello i am Writer just acting like Single cast");
- Logger=new MyClassDelegate.StringDelegate(MyClass.logString);
- ///calling Logger
- Logger("hello i am Logger just acting like Single-cast");
- Transmitter=new MyClassDelegate.StringDelegate(MyClass.TransmitString);
- ///calling Transmitter
- Transmitter("hello i am Transmitter just acting like Single-cast");
- ///here mydelegate used the Combine method of System.MulticastDelegate
- ///and the delegates combine
- myDelegate=(MyClassDelegate.StringDelegate)System.Delegate.
Combine(Writer,Logger);- myDelegate("used Combine");
- ///here Transmitter is also added using the overloaded form of Combine
- myDelegate+=Transmitter;
- myDelegate("Using Overloaded Form");
- ///now using the Remove method
- myDelegate=(MyClassDelegate.StringDelegate)System.Delegate.
Remove(myDelegate,Writer);- myDelegate("Without Writer");
- ///overloaded Remove
- myDelegate-=Transmitter;
- myDelegate("Without Transmitter");
- System.Threading.Thread.Sleep(2300);
- }
- }
- }
面的例子重點是看那兩個已經(jīng)重載的操作符。"-="和"+="。通過上面的例子,你可以清楚的看到C#多路廣播是如何一次代理多個方法的。當(dāng)然你也可以刪除掉那些你不想要的用"-="操作符就可以了。
分享標(biāo)題:C#多路廣播簡單描述
URL標(biāo)題:http://fisionsoft.com.cn/article/dpdgspo.html


咨詢
建站咨詢
