[源码下载]

背水一战 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. bootstrap 坑

    1.  表格内存出不来,也不报错 .. 值是对的..  原因是  table  中必须有属性   data-toggle="table" <table id="My ...

  2. word表格如何实现序号自动填充

    打开word文档,我们需要在如下表格中的准考证号这一列中输入准考证号,手工输入肯定很慢,且容易出错.   我们先选中需要填充准考证号的表格.   选择功能区域中的“开始”,在“段落”组中点击“编号”按 ...

  3. libmysqlclient version

    You probably know that the version number of the libmysqlclient.so library has changed from .16 to . ...

  4. 如何查看Python内置模块的实现代码

    方法1:使用help(random) >>> import random >>> help(random) Help on module random: NAME ...

  5. java 后台 post请求 携带参数 远程操作 调用接口

    package com.huayu.tizong.matchteam.util; import java.io.BufferedReader; import java.io.IOException; ...

  6. 蓝绿部署、红黑部署、AB测试、灰度发布、金丝雀发布、滚动发布的概念与区别(转)

    出处:https://www.baidu.com/link?url=QjboallwNm_jxcL3fHG57wEakiBfAs_3-TChTGu1eBXstlHEsGBc-NDA7AKTqsiroB ...

  7. 3.The significance of Books 书本的意义

    3.The significance of Books 书本的意义 (1)A bookless life is an imcomplete life.Books influence the depth ...

  8. PLSQL Developer对oracle中的数据进行备份恢复

    1.备份数据结构 --进入  工具-->导出用户对象  如图所示 把包括所有者的√去掉,到时候我们就可以随便建一个用户导入数据了,不一定非要scott用户   2.备份数据 工具-->导出 ...

  9. python模块:shutil

    """Utility functions for copying and archiving files and directory trees. XXX The fun ...

  10. python中的\n、\r与\b

    python中使用print函数,有时候会使用end参数来控制字符输出效果,这时候\n.\r与\b就派上用场了. \n 代表换行,也就是从本行换到下一行 \r 代表回车,也就是回到本行最开始的位置,从 ...