//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. BZOJ4294 : [PA2015]Fibonacci

    斐波那契数列模$10^m$的循环节为$6\times10^m$,于是从低位到高位dfs即可. #include<cstdio> #include<cstring> #defin ...

  2. CSS Counters 计数属性

    CSS Counters其实就是一计数器,早期在CSS中计数器仅存在于ul和ol元素.如果要使用在div这样的元素上,只能通过list-style-image或者是元素的backgroud-image ...

  3. java Android SDK安装与环境变量配置以及开发第一个Android程序

    JAVA的安装与环境变量的配置 1.先下载JAVA,并且安装. 下载地址:http://www.oracle.com/technetwork/java/javase/downloads/jdk-7u3 ...

  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. Mac or Centos 下如何编译objective-C

    #import <Foundation/Foundation.h> int main(int argc,const char *argv[]){ @autoreleasepool{ NSL ...

  6. MySQL 用户管理——权限表

    权限表 权限表存放在mysql数据库中 user表结构 用户列:Host.User.Password 权限列:*priv 资源控制列:max* 安全列:其余   db表 存储了用户对某个数据库的操作权 ...

  7. 初识python面向对象

    一.初识python面向对象: class Person: #使用class关键字定义一个类 age=0 #类变量(静态变量) def eat(self,food): #定义一个方法 self.age ...

  8. android之数据库SQLite(一)

    创建数据库 首先定义SQLiteOpenHelper的子类 代码如下: package com.example.myandroid; import android.content.Context; i ...

  9. [转]WinForm和WebForm下读取app.config web.config 中邮件配置的方法

    本文转自:http://blog.csdn.net/jinbinhan/article/details/1598386 1. 在WinForm下读取 App.config中的邮件配置语句如下: Con ...

  10. Hadoop工程包架构解析

    Hadoop源码解析 1 --- Hadoop工程包架构解析 1 Hadoop中各工程包依赖简述    Google的核心竞争技术是它的计算平台.Google的大牛们用了下面5篇文章,介绍了它们的计算 ...