[源码下载]

背水一战 Windows 10 (43) - C# 7.0 新特性

作者:webabcd

介绍
背水一战 Windows 10 之 C# 7.0 新特性

  • 介绍 C# 7.0 的新特性

示例
1、C# 7.0 示例 1: out 变量, 数字语法改进, 值类型的异步返回
CSharp7/Demo1.xaml.cs

/*
* C# 7 示例 1
* out 变量, 数字语法改进, 值类型的异步返回
*/ using System;
using System.Threading.Tasks;
using Windows.UI.Xaml.Controls; namespace Windows10.CSharp7
{
public sealed partial class Demo1 : Page
{
public Demo1()
{
this.InitializeComponent(); sample1();
sample2();
sample3();
} // out 变量(out-variables)
private void sample1()
{
// 这是之前的写法,需要预先声明变量
string s;
OutSample(out s);
lblMsg.Text += s;
lblMsg.Text += Environment.NewLine; // 这是 c#7 的写法,不用预先声明变量了
OutSample(out string ss);
lblMsg.Text += ss;
lblMsg.Text += Environment.NewLine; // 这是 c#7 的写法,不用预先声明变量了,并且可以使用 var
OutSample(out var sss);
lblMsg.Text += sss;
lblMsg.Text += Environment.NewLine;
}
private void OutSample(out string str)
{
str = "xyz"; /*
* 注:
* 1、对于 out 类型来说,是在方法内部初始化的,在 c#7 之前需要在方法外部声明(这显得没有必要,所以有了如今的改进)
* 2、对于 ref 类型来说,是不可能改成 out 这种新方式的,因为 ref 是引用,其作用是方法外部初始化,方法内部改之
*/
} // 数字语法改进(numeric literal syntax improvements)
private void sample2()
{
int a1 = ;
int a2 = 123_456; // 允许数字中出现“_”来提高可读性
lblMsg.Text += a1.ToString();
lblMsg.Text += Environment.NewLine;
lblMsg.Text += a2.ToString();
lblMsg.Text += Environment.NewLine; int b1 = 0xABCDEF;
int b2 = 0xAB_CD_EF; // 允许数字中出现“_”来提高可读性
lblMsg.Text += b1.ToString();
lblMsg.Text += Environment.NewLine;
lblMsg.Text += b2.ToString();
lblMsg.Text += Environment.NewLine;
} // 值类型的异步返回(generalized async return types)
private async void sample3()
{
lblMsg.Text += (await GetNumber1()).ToString();
lblMsg.Text += Environment.NewLine; lblMsg.Text += (await GetNumber2()).ToString();
lblMsg.Text += Environment.NewLine;
}
// 在 c#7 之前异步返回 int 是这么写的,Task 是引用类型
private async Task<int> GetNumber1()
{
await Task.Delay();
return ;
}
// 在 c#7 中异步返回 int 可以这么写,ValueTask 是值类型,提高了效率
// 注:需要通过 nuget 引用 System.Threading.Tasks.Extensions
private async ValueTask<int> GetNumber2()
{
await Task.Delay();
return ;
}
}
}

2、C# 7.0 示例 2: 值类型变量的引用和值类型返回值的引用, 模式匹配, 元组
CSharp7/Demo2.xaml.cs

/*
* C# 7 示例 2
* 值类型变量的引用和值类型返回值的引用, 模式匹配, 元组
*/ using System;
using Windows.UI.Xaml.Controls; namespace Windows10.CSharp7
{
public sealed partial class Demo2 : Page
{
public Demo2()
{
this.InitializeComponent(); sample1();
sample2();
sample3();
} // 值类型变量的引用和值类型返回值的引用(ref locals and returns)
private void sample1()
{
// 值类型变量变为引用类型的示例
int a = ;
ref int b = ref a; // 值类型变量 b 引用了值类型变量 a
a = ;
lblMsg.Text = a.ToString();
lblMsg.Text += Environment.NewLine; // 值类型返回值变为引用类型的示例
int[] array = { , , , , };
ref int x = ref GetByIndex(array, ); // 值类型变量 x 引用了 GetByIndex(array, 2)
x = ;
lblMsg.Text += array[].ToString();
lblMsg.Text += Environment.NewLine;
}
// 返回的值类型变为引用类型
private ref int GetByIndex(int[] array, int index)
{
return ref array[index];
} // 模式匹配(pattern matching)
private void sample2()
{
object a = ;
// 声明 int b,如果 a 是 int 类型则将 a 赋值给 b
if (a is int b)
{
lblMsg.Text += b.ToString();
lblMsg.Text += Environment.NewLine;
} switch (a)
{
// 声明 int c,如果 a 是 int 类型则将 a 赋值给 c,如果 c 大于 0 则执行此 case
case int c when c > :
lblMsg.Text += "case int c when c > 0: " + c;
lblMsg.Text += Environment.NewLine;
break;
// 声明 string c,如果 a 是 string 类型则将 a 赋值给 c
case string c:
lblMsg.Text += "case string c: " + c;
lblMsg.Text += Environment.NewLine;
break;
}
} // 元组(Tuples)
// 注:需要通过 nuget 引用 System.ValueTuple
private void sample3()
{
// Tuples 特性是从 System.Tuple<T1, T2, T3...> 进化而来的
// 注:当有多个返回值时,使用 Tuples 特性是非常方便的 var user1 = GetUser1();
lblMsg.Text += $"{user1.UserId}, {user1.UserName}, {user1.CreateTime}";
lblMsg.Text += Environment.NewLine; var user2 = GetUser2();
lblMsg.Text += $"{user2.UserId}, {user2.UserName}, {user2.CreateTime}";
lblMsg.Text += Environment.NewLine; var user3 = GetUser3();
lblMsg.Text += $"{user3.Item1}, {user3.Item2}, {user3.Item3}";
lblMsg.Text += Environment.NewLine; (int UserId, string UserName, DateTime CreateTime) = GetUser1();
lblMsg.Text += $"{UserId}, {UserName}, {CreateTime}";
lblMsg.Text += Environment.NewLine; var obj1 = (UserId: , UserName: "webabcd");
lblMsg.Text += $"{obj1.UserId}, {obj1.UserName}";
lblMsg.Text += Environment.NewLine; var obj2 = (, "webabcd");
lblMsg.Text += $"{obj2.Item1}, {obj2.Item2}";
lblMsg.Text += Environment.NewLine; (int id, string name) = (, "webabcd");
lblMsg.Text += $"{id}, {name}";
lblMsg.Text += Environment.NewLine;
}
private (int UserId, string UserName, DateTime CreateTime) GetUser1()
{
return (, "webabcd", DateTime.Now);
}
private (int UserId, string UserName, DateTime CreateTime) GetUser2()
{
return (UserId: , UserName: "webabcd", CreateTime: DateTime.Now);
}
private (int, string, DateTime) GetUser3()
{
return (, "webabcd", DateTime.Now);
}
}
}

3、C# 7.0 示例 3: 表达式抛出异常, lambda 表达式作用于构造函数或属性, 局部函数
CSharp7/Demo3.xaml.cs

/*
* C# 7 示例 3
* 表达式抛出异常, lambda 表达式作用于构造函数或属性, 局部函数
*/ using System;
using Windows.UI.Xaml.Controls; namespace Windows10.CSharp7
{
public sealed partial class Demo3 : Page
{
public Demo3()
{
this.InitializeComponent(); sample1();
sample2();
sample3();
} // 表达式抛出异常(throw expressions)
private void sample1()
{
try
{
string a = null;
// 支持在表达式中抛出异常
string b = a ?? throw new Exception("ex");
}
catch (Exception ex)
{
lblMsg.Text += ex.ToString();
lblMsg.Text += Environment.NewLine;
}
} // lambda 表达式作用于构造函数或属性(more expression-bodied members)
// 注:在 c#6 中已经支持了 lambda 表达式作用于字段或方法
private void sample2()
{
MyClass obj = new MyClass("webabcd");
lblMsg.Text += obj.Text;
lblMsg.Text += Environment.NewLine;
}
public class MyClass
{
private string _text; public MyClass(string text) => _text = text; // lambda 表达式作用于构造函数 public string Text // lambda 表达式作用于属性
{
get => _text;
set => _text = value ?? "default text";
}
} // 局部函数(local functions)
private void sample3()
{
int a = GetNumber();
lblMsg.Text += a.ToString();
lblMsg.Text += Environment.NewLine; // 支持局部函数了
int GetNumber()
{
return ;
}
}
}
}

OK
[源码下载]

背水一战 Windows 10 (43) - C# 7.0 新特性的更多相关文章

  1. 背水一战 Windows 10 (1) - C# 6.0 新特性

    [源码下载] 背水一战 Windows 10 (1) - C# 6.0 新特性 作者:webabcd 介绍背水一战 Windows 10 之 C# 6.0 新特性 介绍 C# 6.0 的新特性 示例1 ...

  2. 背水一战 Windows 10 (47) - 控件(ScrollViewer 特性): Chaining, Rail, Inertia, Snap, Zoom

    [源码下载] 背水一战 Windows 10 (47) - 控件(ScrollViewer 特性): Chaining, Rail, Inertia, Snap, Zoom 作者:webabcd 介绍 ...

  3. Hadoop3.0新特性介绍,比Spark快10倍的Hadoop3.0新特性

    Hadoop3.0新特性介绍,比Spark快10倍的Hadoop3.0新特性 Apache hadoop 项目组最新消息,hadoop3.x以后将会调整方案架构,将Mapreduce 基于内存+io+ ...

  4. 背水一战 Windows 10 (83) - 用户和账号: 数据账号的添加和管理, OAuth 2.0 验证

    [源码下载] 背水一战 Windows 10 (83) - 用户和账号: 数据账号的添加和管理, OAuth 2.0 验证 作者:webabcd 介绍背水一战 Windows 10 之 用户和账号 数 ...

  5. 背水一战 Windows 10 (11) - 资源: CustomResource, ResourceDictionary, 加载外部的 ResourceDictionary 文件

    [源码下载] 背水一战 Windows 10 (11) - 资源: CustomResource, ResourceDictionary, 加载外部的 ResourceDictionary 文件 作者 ...

  6. 背水一战 Windows 10 (32) - 控件(选择类): Selector, ComboBox

    [源码下载] 背水一战 Windows 10 (32) - 控件(选择类): Selector, ComboBox 作者:webabcd 介绍背水一战 Windows 10 之 控件(选择类) Sel ...

  7. 背水一战 Windows 10 (22) - 绑定: 通过 Binding 绑定对象, 通过 x:Bind 绑定对象, 通过 Binding 绑定集合, 通过 x:Bind 绑定集合

    [源码下载] 背水一战 Windows 10 (22) - 绑定: 通过 Binding 绑定对象, 通过 x:Bind 绑定对象, 通过 Binding 绑定集合, 通过 x:Bind 绑定集合 作 ...

  8. 背水一战 Windows 10 (17) - 动画: ThemeTransition(过渡效果)

    [源码下载] 背水一战 Windows 10 (17) - 动画: ThemeTransition(过渡效果) 作者:webabcd 介绍背水一战 Windows 10 之 动画 ThemeTrans ...

  9. 背水一战 Windows 10 (16) - 动画: ThemeAnimation(主题动画)

    [源码下载] 背水一战 Windows 10 (16) - 动画: ThemeAnimation(主题动画) 作者:webabcd 介绍背水一战 Windows 10 之 动画 PopInThemeA ...

随机推荐

  1. IOS 小新兵

    2017-07-02 lipo -info BaiduOAuthSDK.a  查看a文件支持的架构 第一个坎: 报错:  未找到模块baiduLogin对应的类BaiduLoginModule.若是自 ...

  2. spring boot 无法启动

    spring boot 使用内置tomcat 报错  : Unable to start embedded Tomcat servlet container Tomcat connector in f ...

  3. js jquery 取得周月年时间

    function formatDate(date) { var myyear = date.getFullYear(); var mymonth = date.getMonth() + 1; var ...

  4. Flex labelFunction 用法

    <mx:VBox horizontalAlign="left" height="100%" width="100%"> < ...

  5. SQL Server 2008中的MERGE(不仅仅是合并)

    SQL Server 2008中的MERGE语句能做很多事情,它的功能是根据源表对目标表执行插入.更新或删除操作.最典型的应用就是进行两个表的同步. 下面通过一个简单示例来演示MERGE语句的使用方法 ...

  6. MVVM中viewmodel的理解

    网上有人写了这段话,我也有同感,特别是第一种用法,很重要,后一种用法,我觉得是把第一种用法加入controller中了. 第一种 “view model” 是 “model for the view” ...

  7. TCP/IP协议(5):传输层之TCP

    一.TCP报文 上图为TCP报文的格式,可以看到TCP头部占20个字节,其中红色圆圈中每一项占一位,表示TCP报文的类型,置1表示该项有效. SYN表示建立连接.    FIN表示关闭连接.    A ...

  8. C# 编码标准(一)

    一直想写一个自己用的代码标准,经过一段时间的优秀开源源码的观察和看其他人写的标准,感觉好的代码给人感觉就是舒服,也非常重要.所以把它们记录归纳总结,以备以后忘记,另外平时写代码的时候可以拿来参考下.下 ...

  9. Docker Swarm集群部署

    一.系统环境 1)服务器环境 节点名称 IP 操作系统 内核版本 manager 172.16.60.95 CentOs7 4.16.1-1.el7.elrepo.x86_64 node-01 172 ...

  10. Java数据库技术

    JDBC即Java数据库连接     是接口,用于执行SQL语句,包含Java写的类和界面.几乎可以把SQL传给任何数据库,不用单独编写SQL.     用处,1是与数据库建立连接,2是向数据库发送S ...