//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示例的更多相关文章

  1. .Net Core 1.0.0 RC2安装及示例教程

    前几天微软发布了.Net Core1.0.0 RC2 Preview版本,一直都想尝试下跨平台的.Net Core,一直拖到今天,也参考了下园友们的经验,闲时整理了一下安装的步骤,供大家参考. 我们要 ...

  2. RSS介绍、RSS 2.0规范说明和示例代码

    RSS是一种消息来源格式规范,用以发布经常更新资料的网站,例如博客.新闻的网摘.RSS文件,又称做摘要.网摘.更新.频道等,包含了全文或节选文字,再加上一定的属性数据.RSS让发布者自动发布信息,也使 ...

  3. OAuth 2.0 Server PHP实现示例

    需求实现三方OAuth2.0授权登录 使用OAuth服务OAuth 2.0 Server PHP 环境nginx mysqlphp 框架Yii 一 安装 项目目录下安装应用 composer.phar ...

  4. kafka producer 0.8.2.1 示例

    package test_kafka; import java.util.Properties; import java.util.concurrent.atomic.AtomicInteger; i ...

  5. kafka consumer 0.8.2.1示例代码

    package test_kafka; import java.util.ArrayList; import java.util.HashMap; import java.util.List; imp ...

  6. SkylineGlobe 7.0版本 矢量数据查询示例代码

    在Pro7.0.0和7.0.1环境下测试可用. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ...

  7. 微信OAuth2.0网页授权php示例

    1.配置授权回调页面域名,如 www.aaa.com 2.模拟公众号的第三方网页,fn_system.php <?php if(empty($_SESSION['user'])){ header ...

  8. 部署Bookinfo示例程序详细过程和步骤(基于Kubernetes集群+Istio v1.0)

    部署Bookinfo示例程序详细过程和步骤(基于Kubernetes集群+Istio v1.0) 部署Bookinfo示例程序   在下载的Istio安装包的samples目录中包含了示例应用程序. ...

  9. C#7.0中有哪些新特性?

    以下将是 C# 7.0 中所有计划的语言特性的描述.随着 Visual Studio “15” Preview 4 版本的发布,这些特性中的大部分将活跃起来.现在是时候来展示这些特性,你也告诉借此告诉 ...

随机推荐

  1. Sprint第一个冲刺(第十一天)

    一.Sprint介绍 修改登录信息界面(修改用户名.密码.邮箱.电话.年龄),且同步到云端:修改Item布局:增添设置页. 实验截图: 任务进度: 二.Sprint周期 看板: 燃尽图:

  2. Cyclic Nacklace[HDU3746]

    Cyclic Nacklace Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  3. 在线代码格式化,在线JSON校验格式化

    在线代码格式化 http://tool.oschina.net/codeformat/json 在线JSON校验格式化 http://www.kjson.com/ 两个好用工具

  4. Centos6.4 yum安装MariaDB5.5

    vi /etc/yum.repos.d/MariaDB.repo 加入下面内容 [mariabd]name=MariaDBbaseurl=http://yum.mariadb.org/5.5.34/c ...

  5. TopCoder SRM 588 DIV2 KeyDungeonDiv2

    简单的题目 class KeyDungeonDiv2 { public: int countDoors(vector <int> doorR, vector <int> doo ...

  6. TYVJ P1093 验证数独 Label:none

    背景 XX学校风靡一款智力游戏,也就是数独(九宫格),先给你一个数独,并需要你验证是否符合规则. 描述 具体规则如下:每一行都用到1,2,3,4,5,6,7,8,9,位置不限,每一列都用到1,2,3, ...

  7. 【UR #4】元旦三侠的游戏(博弈论+记忆化)

    http://uoj.ac/contest/6/problem/51 题意:给m($m \le 10^5$)个询问,每次给出$a, b(a^b \le n, n \le 10^9)$,对于每一组$a, ...

  8. C#中使用JQueryUI中Autocomplete插件

    服务器端后台代码: 1 private string GetModelNames() { 2 return @"[ 3 { 4 'value': 'jquery', 5 'label': ' ...

  9. DOS命令下输入:java Hello 出现以下几种结果可能的原因:

    DOS命令下输入:java Hello 出现以下结果:Bad command or the file name 没有这个命令或文件名 原因可能是没有成功安装jdk或者没有配置好jdk 的环境变量,或者 ...

  10. 数据库存储txt文本和jpg图片

    环境:MySql+SQLyog+j2se+jdbc 存储文本用longtext类型 存储图片用blob类型 1.首先建表 create table t_t (id int(16) NOT NULL A ...