背水一战 Windows 10 (1) - C# 6.0 新特性
作者:webabcd
介绍
背水一战 Windows 10 之 C# 6.0 新特性
- 介绍 C# 6.0 的新特性
示例
1、C# 6.0 示例 1: 自动属性支持初始化, 字符串嵌入的新方式, 通过 Using Static 引用静态类, nameof 表达式
CSharp6/Demo1.xaml.cs
/*
* C# 6 示例 1
* 自动属性支持初始化, 字符串嵌入的新方式, 通过 Using Static 引用静态类, nameof 表达式
*/ using System;
using System.ComponentModel;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using static System.Math; // 通过 Using Static 引用静态类 namespace Windows10.CSharp6
{
public sealed partial class Demo1 : Page
{
// 自动属性支持初始化了
public string MyName { get; set; } = "default value";
// 只读自动属性也可以初始化
public int MyAge { get; } = ; public Demo1()
{
this.InitializeComponent(); this.Loaded += Demo1_Loaded;
} private void Demo1_Loaded(object sender, RoutedEventArgs e)
{
sample1();
sample2();
sample3();
sample4();
} // 自动属性支持初始化(Initializers for auto-properties)
private void sample1()
{
lblMsg.Text = this.MyName;
lblMsg.Text += Environment.NewLine; lblMsg.Text += this.MyAge.ToString();
lblMsg.Text += Environment.NewLine;
} // 字符串嵌入(String Interpolation)的新方式
private void sample2()
{
// 之前的字符串嵌入方式
lblMsg.Text += string.Format("myName: {0}, myAge: {1}", this.MyName, this.MyAge);
lblMsg.Text += Environment.NewLine; // 新的字符串嵌入方式
lblMsg.Text += $"myName: {this.MyName}, myAge: {this.MyAge}, {{this.MyName}}";
lblMsg.Text += Environment.NewLine;
} // 通过 Using Static 引用静态类
private void sample3()
{
// 之前通过 using static System.Math; 引用了静态类 System.Math
// 那么之后就可以直接使用 System.Math 的方法了,如下
lblMsg.Text += Abs(-).ToString();
lblMsg.Text += Environment.NewLine;
} // nameof 表达式
private void sample4()
{
DateTime dateTime = new DateTime();
// nameof 表达式 - 用于获取变量的名称,比如下面这个会输出 "dateTime",这个有什么用呢?参见之后的 "Book" 类的说明
lblMsg.Text += nameof(dateTime);
lblMsg.Text += Environment.NewLine;
}
// 演示 nameof 表达式的用途
public class Book : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged; private string _title;
public string Title
{
get { return _title; }
set
{
_title = value; if (PropertyChanged != null)
{
// 这里以前只能这么写 PropertyChanged(this, new PropertyChangedEventArgs("Title"));
// 现在可以向下面这样写
PropertyChanged(this, new PropertyChangedEventArgs(nameof(Title)));
// 有什么用呢?
// 如果我要修改属性 Title 的名字时,而又忘了修改对应的 PropertyChangedEventArgs 中的名字,则编译会报错,以便修改
// 当然修改属性名字时最好用 Visual Studio 提供的“重命名”的方法
}
}
}
}
}
}
2、C# 6.0 示例 2: 在 catch 和 finally 中支持 await, 异常过滤器
CSharp6/Demo2.xaml.cs
/*
* C# 6 示例 2
* 在 catch 和 finally 中支持 await, 异常过滤器
*/ using System;
using System.Threading.Tasks;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls; namespace Windows10.CSharp6
{
public sealed partial class Demo2 : Page
{
public Demo2()
{
this.InitializeComponent(); this.Loaded += Demo2_Loaded;
} private void Demo2_Loaded(object sender, RoutedEventArgs e)
{
sample1();
sample2();
} // 在 catch 和 finally 中也支持 await 了
private async void sample1()
{
try
{
throw new Exception("");
}
catch
{
await Task.Delay();
}
finally
{
await Task.Delay();
}
} // 异常过滤器 (Exception filters)
private void sample2()
{
try
{
throw new Exception(new Random().Next().ToString());
}
catch (Exception ex) when (ex.Message.Equals("")) // 通过 when 表达式过滤异常
{
lblMsg.Text += "";
lblMsg.Text += Environment.NewLine;
}
catch (Exception ex) when (ex.Message.Equals("")) // 通过 when 表达式过滤异常
{
lblMsg.Text += "";
lblMsg.Text += Environment.NewLine;
}
catch (Exception ex) when (CheckExceptionMessage(ex, "")) // 通过 when 表达式过滤异常(表达式中的判断条件也可以是一个方法调用)
{
lblMsg.Text += "";
lblMsg.Text += Environment.NewLine;
}
}
private bool CheckExceptionMessage(Exception ex, string value)
{
if (ex.Message.Equals(value))
return true;
return false;
}
}
}
3、C# 6.0 示例 3: 带索引的对象初始化器, null 值判断, lambda 表达式作用于属性或方法
CSharp6/Demo3.xaml.cs
/*
* C# 6 示例 3
* 带索引的对象初始化器, null 值判断, lambda 表达式作用于字段或方法
*/ using System;
using System.Collections.Generic;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls; namespace Windows10.CSharp6
{
public sealed partial class Demo3 : Page
{
public Demo3()
{
this.InitializeComponent(); this.Loaded += Demo3_Loaded;
} private void Demo3_Loaded(object sender, RoutedEventArgs e)
{
sample1();
sample2();
sample3();
} // 带索引的对象初始化器
private void sample1()
{
// Dictionary 也可以这样初始化了
var dict = new Dictionary<int, string>
{
[] = "seven",
[] = "nine",
[] = "thirteen"
}; lblMsg.Text += dict[].ToString();
lblMsg.Text += Environment.NewLine;
} // null 值判断
private void sample2()
{
List<int> list = null;
int? count = list?.Count; // 因为 list 是 null,所以 list?.Count 是 null
int? value3 = list?[]; // 因为 list 是 null,所以 list?[3] 是 null list = new List<int> { , , };
count = list?.Count; // 这句会异常的,因为 list 不是 null 且 list 没有第 11 个元素
// int? value10 = list?[10]; lblMsg.Text += count.ToString();
lblMsg.Text += Environment.NewLine; // null 值判断的最主要的应用是这样的
// 之前的写法
object obj1 = null;
if (obj1 != null)
{
obj1.ToString();
}
// 现在的写法
object obj2 = null;
obj2?.ToString();
} // lambda 表达式作用于字段或方法
private void sample3()
{
lblMsg.Text += this.ToString();
lblMsg.Text += Environment.NewLine; lblMsg.Text += this.FullName;
lblMsg.Text += Environment.NewLine;
} public string FirstName { get; set; } = "lei";
public string LastName { get; set; } = "wanglei"; public override string ToString() => $"{FirstName} {LastName}"; // lambda 表达式作用于方法
public string FullName => $"{FirstName} {LastName}"; // lambda 表达式作用于字段
}
}
OK
[源码下载]
背水一战 Windows 10 (1) - C# 6.0 新特性的更多相关文章
- 背水一战 Windows 10 (43) - C# 7.0 新特性
[源码下载] 背水一战 Windows 10 (43) - C# 7.0 新特性 作者:webabcd 介绍背水一战 Windows 10 之 C# 7.0 新特性 介绍 C# 7.0 的新特性 示例 ...
- 背水一战 Windows 10 (47) - 控件(ScrollViewer 特性): Chaining, Rail, Inertia, Snap, Zoom
[源码下载] 背水一战 Windows 10 (47) - 控件(ScrollViewer 特性): Chaining, Rail, Inertia, Snap, Zoom 作者:webabcd 介绍 ...
- Hadoop3.0新特性介绍,比Spark快10倍的Hadoop3.0新特性
Hadoop3.0新特性介绍,比Spark快10倍的Hadoop3.0新特性 Apache hadoop 项目组最新消息,hadoop3.x以后将会调整方案架构,将Mapreduce 基于内存+io+ ...
- 背水一战 Windows 10 (83) - 用户和账号: 数据账号的添加和管理, OAuth 2.0 验证
[源码下载] 背水一战 Windows 10 (83) - 用户和账号: 数据账号的添加和管理, OAuth 2.0 验证 作者:webabcd 介绍背水一战 Windows 10 之 用户和账号 数 ...
- 背水一战 Windows 10 (11) - 资源: CustomResource, ResourceDictionary, 加载外部的 ResourceDictionary 文件
[源码下载] 背水一战 Windows 10 (11) - 资源: CustomResource, ResourceDictionary, 加载外部的 ResourceDictionary 文件 作者 ...
- 背水一战 Windows 10 (32) - 控件(选择类): Selector, ComboBox
[源码下载] 背水一战 Windows 10 (32) - 控件(选择类): Selector, ComboBox 作者:webabcd 介绍背水一战 Windows 10 之 控件(选择类) Sel ...
- 背水一战 Windows 10 (22) - 绑定: 通过 Binding 绑定对象, 通过 x:Bind 绑定对象, 通过 Binding 绑定集合, 通过 x:Bind 绑定集合
[源码下载] 背水一战 Windows 10 (22) - 绑定: 通过 Binding 绑定对象, 通过 x:Bind 绑定对象, 通过 Binding 绑定集合, 通过 x:Bind 绑定集合 作 ...
- 背水一战 Windows 10 (17) - 动画: ThemeTransition(过渡效果)
[源码下载] 背水一战 Windows 10 (17) - 动画: ThemeTransition(过渡效果) 作者:webabcd 介绍背水一战 Windows 10 之 动画 ThemeTrans ...
- 背水一战 Windows 10 (16) - 动画: ThemeAnimation(主题动画)
[源码下载] 背水一战 Windows 10 (16) - 动画: ThemeAnimation(主题动画) 作者:webabcd 介绍背水一战 Windows 10 之 动画 PopInThemeA ...
随机推荐
- Node.js刷新session过期时间
在Node.js中,我们通常使用express-session这个包来使用和管理session,保存服务端和客户端浏览器之间的会话状态.那如何才能实现当用户刷新当前页面或者点击页面上的按钮时重新刷新s ...
- java系统性能分析
netstat -ano | findstr 31900 注意最后是pid 堆栈的作用: 线程死锁分析 辅助CPU过高分析 线程资源不足分析 性能瓶颈分析 关键线程异常退出 Windows:在运行ja ...
- Properties
java.util 类 Properties 因为 Properties 继承于 Hashtable,所以可对 Properties 对象应用 put 和 putAll 方法.但强烈反对使用这两个方法 ...
- CSS水平垂直居中的几种方法
直接进入主题! 一.脱离文档流元素的居中 方法一:margin:auto法 CSS代码: div{ width: 400px; height: 400px; position: relative; b ...
- PDO事务处理
PDO事务处理 2014-9-3 10:44:19 By jiancaigege==================================== 概要:将多条sql操作(增删改)作为一个操作单 ...
- 状态栏 a.getBoolean(1, false) 报错
状态栏 a.getBoolean(1, false) 报错 这个错误在编译运行时候并不会出现,但是当需要编译打包的时候,就会报出这个异常. TypedArray a = mContext.obtain ...
- 关于stm32的正交解码
关于正交解码,我先解释何为正交解码,,,,其实名字挺高大上的,,,,还是先说编码器吧 看一下我用过的一种编码器 编码器的 线 数 ,是说编码器转一圈输出多少个脉冲,,,如果一个编码器是500线,,,说 ...
- salesforce 零基础学习(三十九) soql函数以及常量
在salesforce中,我们做SOQL查询时,往往需要用到计算式,比如求和,求平均值,或者过滤数据时,往往需要通过时间日期过滤,SOQL已经封装了很多的函数,可以更加方便我们的sql查询而不需要自己 ...
- Android Activity 常用技巧
1.设置 Activity 背景色为透明 在style.xml里面声明: <style name="TranslucentActivityStyle" parent=&quo ...
- 开源项目IPProxys的使用
前几天看了一下github上,IPProxys开源项目(https://github.com/qiyeboy/IPProxys)快100star了,看来大家对这个项目还是比较感兴趣的.最近一直没更新文 ...