C#5.0 .net 4.5示例
- //filename: MathOperations.cs
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace TestAsyncAwait
- {
- public class MathOperations
- {
- public static double MultiplyByTwo(double d)
- {
- return d * 2;
- }
- public static double Square(double d)
- {
- return d * d;
- }
- }
- }
- //filename: MyClass.cs
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- namespace TestAsyncAwait
- {
- public class MyClass
- {
- public MyClass()
- {
- //DisplayValue();
- DisplayValueWithContinuationTask();
- Console.WriteLine("MyClass() end.");
- }
- public Task<double> GetValueAsync(double num1, double num2)
- {
- return Task.Run<double>(() => {
- Thread.Sleep(1000);
- return num1 + num2;
- });
- }
- public async void DisplayValue()
- {
- double result = await GetValueAsync(1, 2);
- Console.WriteLine("result is :" + result);
- }
- public void DisplayValueWithContinuationTask()
- {
- Task<double> task = GetValueAsync(1, 2);
- task.ContinueWith(t =>
- {
- double result = t.Result;
- Console.WriteLine("result is :" + result);
- });
- }
- }
- }
- //filename: Program.cs
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- namespace TestAsyncAwait
- {
- class Program
- {
- public static Tuple<int, int> Divide(int dividend, int divisor)
- {
- int result = dividend / divisor;
- int reminder = dividend % divisor;
- return Tuple.Create<int, int>(result, reminder);
- }
- private delegate string GetAString();
- static void ProcessAndDispalyNumber(Func<double,double> action, double value){
- double result = action(value);
- Console.WriteLine(string.Format("value={0},result={1}", value, result));
- }
- #region 异步编程实验
- static string Greeting(string name)
- {
- Thread.Sleep(3000);
- return string.Format("threadID:[{0}] says: Hello, {1}!", Thread.CurrentThread.ManagedThreadId, name);
- }
- static Task<string> GreetingAsync(string name)
- {
- return Task.Run<string>(() => {
- return Greeting(name);
- });
- }
- async static void CallerWithAsync()
- {
- string result = await GreetingAsync("张三");
- Console.WriteLine(result);
- }
- static void CallerWithContinuationWith()
- {
- Task<string> task = GreetingAsync("张三");
- task.ContinueWith(t => {
- string result = t.Result;
- Console.WriteLine(result);
- });
- }
- async static void MultipleAsyncMethods()
- {
- string result1 = await GreetingAsync("张三@");
- string result2 = await GreetingAsync("李四@");
- Console.WriteLine("Finished with 2 result: {0},{1}", result1, result2);
- }
- async static void MultipleAsyncMethodsWithCombinators1()
- {
- Task<string> t1 = GreetingAsync("张三1");
- Task<string> t2 = GreetingAsync("李四1");
- await Task.WhenAll(t1, t2);
- Console.WriteLine("Finished with 2 result: {0},{1}", t1.Result, t2.Result);
- }
- async static void MultipleAsyncMethodsWithCombinators2()
- {
- Task<string> t1 = GreetingAsync("张三2");
- Task<string> t2 = GreetingAsync("李四2");
- string[] result = await Task.WhenAll(t1, t2);
- Console.WriteLine("Finished with 2 result: {0},{1}", result[0], result[1]);
- }
- #endregion
- static void Main(string[] args)
- {
- /*
- CallerWithAsync();
- CallerWithContinuationWith();
- MultipleAsyncMethods();
- MultipleAsyncMethodsWithCombinators1();
- MultipleAsyncMethodsWithCombinators2();
- Console.WriteLine(Thread.CurrentThread.ManagedThreadId + ": Done.");
- */
- ParallelLoopResult result = Parallel.For(0, 10, async i => {
- Console.WriteLine("{0}, task: {1}, thread: {2}", i, Task.CurrentId,
- Thread.CurrentThread.ManagedThreadId);
- //Thread.Sleep(10);
- await Task.Delay(10);
- Console.WriteLine("{0}, task: {1}, thread: {2}", i, Task.CurrentId,
- Thread.CurrentThread.ManagedThreadId);
- });
- Console.WriteLine("Is completed: {0}", result.IsCompleted);
- /*
- MyClass mc = new MyClass();
- Console.WriteLine("-----------------");
- var result = Divide(5, 2);
- Console.WriteLine(string.Format("result={0},reminder={1}",result.Item1, result.Item2));
- int x = 4;
- //GetAString func = new GetAString(x.ToString);
- GetAString func = x.ToString;
- Console.WriteLine(func());
- Console.WriteLine(func.Invoke());
- Func<double, double>[] operations =
- {
- MathOperations.MultiplyByTwo,
- MathOperations.Square
- };
- foreach (var item in operations)
- {
- ProcessAndDispalyNumber(item, 3.14);
- }
- string midPart = ", middle part,";
- Func<string, string> anonDel = delegate(string value) {
- string s = value + midPart;
- s += " last part";
- return s;
- };
- Console.WriteLine(anonDel("Hello"));
- Func<string, string> anonDel2 = (string value) =>
- {
- string s = value + midPart;
- s += " last part. version 2";
- return s;
- };
- Console.WriteLine(anonDel2("Hello"));
- Func<string, string> anonDel3 = value =>
- {
- string s = value + midPart;
- s += " last part. version 3";
- return s;
- };
- Console.WriteLine(anonDel3("Hello"));
- int someValue = 5;
- Func<int, int> f = p1 => p1 + someValue;
- Console.WriteLine(f(1));
- someValue = 6;
- Console.WriteLine(f(1));
- //var lists = new List<string>() { "1","2"};
- dynamic dyn;
- dyn = 100;
- Console.WriteLine(dyn.GetType());
- Console.WriteLine(dyn);
- dyn = "abc中国";
- Console.WriteLine(dyn.GetType());
- Console.WriteLine(dyn);
- */
- Console.ReadKey();
- }
- }
- }
C#5.0 .net 4.5示例的更多相关文章
- .Net Core 1.0.0 RC2安装及示例教程
前几天微软发布了.Net Core1.0.0 RC2 Preview版本,一直都想尝试下跨平台的.Net Core,一直拖到今天,也参考了下园友们的经验,闲时整理了一下安装的步骤,供大家参考. 我们要 ...
- RSS介绍、RSS 2.0规范说明和示例代码
RSS是一种消息来源格式规范,用以发布经常更新资料的网站,例如博客.新闻的网摘.RSS文件,又称做摘要.网摘.更新.频道等,包含了全文或节选文字,再加上一定的属性数据.RSS让发布者自动发布信息,也使 ...
- OAuth 2.0 Server PHP实现示例
需求实现三方OAuth2.0授权登录 使用OAuth服务OAuth 2.0 Server PHP 环境nginx mysqlphp 框架Yii 一 安装 项目目录下安装应用 composer.phar ...
- kafka producer 0.8.2.1 示例
package test_kafka; import java.util.Properties; import java.util.concurrent.atomic.AtomicInteger; i ...
- kafka consumer 0.8.2.1示例代码
package test_kafka; import java.util.ArrayList; import java.util.HashMap; import java.util.List; imp ...
- SkylineGlobe 7.0版本 矢量数据查询示例代码
在Pro7.0.0和7.0.1环境下测试可用. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ...
- 微信OAuth2.0网页授权php示例
1.配置授权回调页面域名,如 www.aaa.com 2.模拟公众号的第三方网页,fn_system.php <?php if(empty($_SESSION['user'])){ header ...
- 部署Bookinfo示例程序详细过程和步骤(基于Kubernetes集群+Istio v1.0)
部署Bookinfo示例程序详细过程和步骤(基于Kubernetes集群+Istio v1.0) 部署Bookinfo示例程序 在下载的Istio安装包的samples目录中包含了示例应用程序. ...
- C#7.0中有哪些新特性?
以下将是 C# 7.0 中所有计划的语言特性的描述.随着 Visual Studio “15” Preview 4 版本的发布,这些特性中的大部分将活跃起来.现在是时候来展示这些特性,你也告诉借此告诉 ...
随机推荐
- 10分钟API Hook MessageBox
10分钟API Hook MessageBox 分类: C++2012-04-12 22:52 877人阅读 评论(4) 收藏 举报 hookwinapidllthreadpython编程 转载注明出 ...
- POJ 3686 & 拆点&KM
题意: 有n个订单,m个工厂,第i个订单在第j个工厂生产的时间为t[i][j],一个工厂可以生产多个订单,但一次只能生产一个订单,也就是说如果先生产a订单,那么b订单要等到a生产完以后再生产,问n个订 ...
- String, StringBuffer, StringBuilder(转载)
http://blog.csdn.net/rmn190/article/details/1492013 String 字符串常量StringBuffer 字符串变量(线程安全)StringBuilde ...
- ARC指南2 - ARC的开启和禁止
要想将非ARC的代码转换为ARC的代码,大概有2种方式: 1.使用Xcode的自动转换工具 2.手动设置某些文件支持ARC 一.Xcode的自动转换工具 Xcode带了一个自动转换工具,可以将旧的源代 ...
- redis 配置 linux
附件 启动停止脚本 redis_6433: #/bin/sh #Configurations injected by install_server below.... EXEC=/usr/local/ ...
- JavaScript操作DOM对象
js的精华即是操作DOM对象 [1]先看代码 <!DOCTYPE html> <html> <head> <meta charset="UTF-8& ...
- PAT天梯赛练习题 L2-013 红色警报(并查集+逆序加边)
L2-013. 红色警报 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 战争中保持各个城市间的连通性非常重要.本题要求你编写一 ...
- 《Java核心技术卷二》笔记(二)文件操作和内存映射文件
文件操作 上一篇已经总结了流操作,其中也包括文件的读写.文件系统除了读写以为还有很多其他的操作,如复制.移动.删除.目录浏览.属性读写等.在Java7之前,一直使用File类用于文件的操作.Java7 ...
- Apache Spark源码走读之14 -- Graphx实现剖析
欢迎转载,转载请注明出处,徽沪一郎. 概要 图的并行化处理一直是一个非常热门的话题,这里头的重点有两个,一是如何将图的算法并行化,二是找到一个合适的并行化处理框架.Spark作为一个非常优秀的并行处理 ...
- Apache Spark源码走读之8 -- Spark on Yarn
欢迎转载,转载请注明出处,徽沪一郎. 概要 Hadoop2中的Yarn是一个分布式计算资源的管理平台,由于其有极好的模型抽象,非常有可能成为分布式计算资源管理的事实标准.其主要职责将是分布式计算集群的 ...