C#中的关键字学习:delegate(委托)和volatile

来源:互联网 发布:有了源码如何建站 编辑:程序博客网 时间:2024/05/21 18:46

MSDN:http://msdn.microsoft.com/zh-cn/library/x53a06bb.aspx

1. delegate

delegate 是一种可用于封装命名或匿名方法的引用类型。

通过将委托与命名方法或匿名方法关联,可以实例化委托。

参考代码:

// 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: {0}", multByTwo);        // Instantiate delegate with anonymous method:        MathAction ma2 = delegate(double input)        {            return input * input;        };        double square = ma2(5);        Console.WriteLine("square: {0}", square);        // Instantiate delegate with lambda expression        MathAction ma3 = s => s * s * s;        double cube = ma3(4.375);        Console.WriteLine("cube: {0}", cube);    }    // Output:    // multByTwo: 9    // square: 25    // cube: 83.740234375}

2. volatile

volatile 关键字指示一个字段可以由多个同时执行的线程修改。

代码:

using System;using System.Threading;public class Worker{    // This method is called when the thread is started.    public void DoWork()    {        while (!_shouldStop)        {            Console.WriteLine("Worker thread: working...");        }        Console.WriteLine("Worker thread: terminating gracefully.");    }    public void RequestStop()    {        _shouldStop = true;    }    // Keyword volatile is used as a hint to the compiler that this data    // member is accessed by multiple threads.    private volatile bool _shouldStop;}public class WorkerThreadExample{    static void Main()    {        // Create the worker thread object. This does not start the thread.        Worker workerObject = new Worker();        Thread workerThread = new Thread(workerObject.DoWork);        // Start the worker thread.        workerThread.Start();        Console.WriteLine("Main thread: starting worker thread...");        // Loop until the worker thread activates.        while (!workerThread.IsAlive) ;        // Put the main thread to sleep for 1 millisecond to        // allow the worker thread to do some work.        Thread.Sleep(1);        // Request that the worker thread stop itself.        workerObject.RequestStop();        // Use the Thread.Join method to block the current thread         // until the object's thread terminates.        workerThread.Join();        Console.WriteLine("Main thread: worker thread has terminated.");    }    // Sample output:    // Main thread: starting worker thread...    // Worker thread: working...    // Worker thread: working...    // Worker thread: working...    // Worker thread: working...    // Worker thread: working...    // Worker thread: working...    // Worker thread: terminating gracefully.    // Main thread: worker thread has terminated.}


0 0