MSDN 样例代码收集

在学习过程中发现的一些好的样例代码,收集在这里便于查看!

 


  • 下面是关于Delegate的样例代码:
    引用地址:http://msdn.microsoft.com/zh-cn/library/900fyy8e.aspx

  • // Declare delegate -- defines required signature:
    delegate double MathAction(double num);
    
    class DelegateTest
    {
        // Regular method that matches signature:
        static double Double(double input)
        {
            return input * 2;
        }
    
        static void Main()
        {
            // Instantiate delegate with named method:
            MathAction ma = Double;
    
            // Invoke delegate ma:
            double multByTwo = ma(4.5);
            Console.WriteLine(multByTwo);
    
            // Instantiate delegate with anonymous method:
            MathAction ma2 = delegate(double input)
            {
                return input * input;
            };
    
            double square = ma2(5);
            Console.WriteLine(square);
    
            // Instantiate delegate with lambda expression
            MathAction ma3 = s => s * s * s;
            double cube = ma3(4.375);
    
            Console.WriteLine(cube);
        }
    }
    


  •  下面是关于事件(event)的样例代码: 
    Address: http://msdn.microsoft.com/zh-cn/library/w369ty8x.aspx
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;   
    
    using System.Collections.Generic;
    
    namespace study_CC.DotNetEvents
    {
    
    
        // Define a class to hold custom event info
        public class CustomEventArgs : EventArgs
        {
            public CustomEventArgs(string s)
            {
                message = s;
            }
            private string message;
    
            public string Message
            {
                get { return message; }
                set { message = value; }
            }
        }
    
        // Class that publishes an event
        class Publisher
        {
    
            // Declare the event using EventHandler<T>
            public event EventHandler<CustomEventArgs> RaiseCustomEvent;
    
            public void DoSomething()
            {
                // Write some code that does something useful here
                // then raise the event. You can also raise an event
                // before you execute a block of code.
                OnRaiseCustomEvent(new CustomEventArgs("Did something"));
    
            }
    
            // Wrap event invocations inside a protected virtual method
            // to allow derived classes to override the event invocation behavior
            protected virtual void OnRaiseCustomEvent(CustomEventArgs e)
            {
                // Make a temporary copy of the event to avoid possibility of
                // a race condition if the last subscriber unsubscribes
                // immediately after the null check and before the event is raised.
                EventHandler<CustomEventArgs> handler = RaiseCustomEvent;
    
                // Event will be null if there are no subscribers
                if (handler != null)
                {
                    // Format the string to send inside the CustomEventArgs parameter
                    e.Message += String.Format(" at {0}", DateTime.Now.ToString());
    
                    // Use the () operator to raise the event.
                    handler(this, e);
                }
            }
        }
    
        //Class that subscribes to an event
        class Subscriber
        {
            private string id;
            public Subscriber(string ID, Publisher pub)
            {
                id = ID;
                // Subscribe to the event using C# 2.0 syntax
                pub.RaiseCustomEvent += HandleCustomEvent;
            }
    
            // Define what actions to take when the event is raised.
            void HandleCustomEvent(object sender, CustomEventArgs e)
            {
                Console.WriteLine(id + " received this message: {0}", e.Message);
            }
        }
    
        class Program
        {
            static void Main(string[] args)
            {
                Publisher pub = new Publisher();
                Subscriber sub1 = new Subscriber("sub1", pub);
                Subscriber sub2 = new Subscriber("sub2", pub);
    
                // Call the method that raises the event.
                pub.DoSomething();
    
                // Keep the console window open
                Console.WriteLine("Press Enter to close this window.");
                Console.ReadLine();
    
            }
        }
    }
    


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值