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
- C#中的关键字学习:delegate(委托)和volatile
- C#中的委托(Delegate)
- c#中的delegate(委托)和event(事件)
- c#中的delegate(委托)和event(事件)
- c#中的delegate(委托)和event(事件)
- c#中的delegate(委托)和event(事件)
- c#中的delegate(委托)和event(事件)
- c#中的delegate(委托)和event(事件)
- c#中的delegate(委托)和event(事件)
- c#中的delegate(委托)和event(事件)了
- c#中的delegate(委托)和event(事件)
- c#中的delegate(委托)和event(事件)
- C#中的委托(Delegate)和事件(Event)
- C#中的委托(Delegate)和事件(Event)
- C#中的委托(Delegate)和事件(Event)
- C#中的委托delegate
- C#中的委托Delegate
- delegate C#关键字 (委托类型)
- oj学生数组
- Ural 1519. Formula 1 优美的插头DP
- zookeeper的领导者选举和原子广播
- Object Literals(提供了很棒的语法糖)
- c htoi
- C#中的关键字学习:delegate(委托)和volatile
- 学习理论和动手实践
- 面试杂题(七)二维数组中寻找
- POJ 2362 Square(DFS回溯构造)
- highcharts图表高级入门之polar:极地图的基本配置以及一些关键配置说明(转载)
- Android WebView 与HttpClient 共用本地cookie问题
- 加快冯就卡而后士大夫人体后熬过
- Java Socket实战之六 使用NIO包实现Socket通信
- Office 2013 双激活状态