c# 6.0 语法特性
2024-10-07 16:41:33
namespace _6._0新特性
{
using static _6._0新特性.Statics.StaticClass;
class Program
{
static void Main(string[] args)
{
// 特性01 静态的using 声明
//之前的调用方式 类名 +方法名
_6._0新特性.Statics.StaticClass.TestFunc();
// 在加了 using static _6._0新特性.Statics.StaticClass; 引用之后,直接调用 ,如下
TestFunc();
}
}
// 特性02 表达式体方法,如下简写方式
//public string GetNewString(string str)
//{
// return $"{str}newString";
//}
public string GetNewString(string str) => $"{str}newString";
//调用上面的方法
public string GetNewString1(string str) => GetNewString("");
//特性3 Get 方法的写法
private string _name;
public string Name => _name;
//特性4 Set 方法 设定初始值
public string Age { get; set; } = "";
public int Age1 { get; set; } = default(Int32);
//特性05 只读自动属性
private readonly string _sex;
public string Sex => _sex;
static void Main(string[] args)
{ //特性06 nameof
Console.WriteLine($"{nameof(Main)},{nameof(args)}"); //输出 Main,args
}
//特性07
//if (str != null)
// return str.ToString() + "22";
//else return str;
// 等同于下面的语法
return str?.ToString() + "";
// 特性08 字典得初始化
var dic = new Dictionary<string, string>();
dic.Add("", "");
// 以下新的写法
var dict = new Dictionary<string, string>
{
[""] = "",
[""] = ""
};
// 特性09 异常的过滤
try
{ }
catch (Exception ex) when (ex.GetType() == typeof(ArgumentException))
{
//
}
// 特性10 可以在 catch 中使用Await 关键字
最新文章
- 传输层(3)-缓冲区大小及限制、TCP输出
- ECharts-基于Canvas,纯Javascript图表库,提供直观,生动,可交互,可个性化定制的数据可视化图表
- September 2nd 2016 Week 36th Friday
- Redis在Windows下的安装和使用
- KeyBord事件分发和接收简要过程代码示例
- DISPLAY_ITEM built-in in Oracle D2k Forms
- 【转】C/C++除法实现方式及负数取模详解
- ASP.NET 资料下载
- (转)ios跳转到通用页面
- 通过取父级for循环的i来理解闭包,iife,匿名函数
- 图论基础之Dijkstra算法的初探
- android 网络获取json并且显示(2)
- 消息队列_MSMQ(2)简单应用
- go语言环境安装
- (FZU 2150) Fire Game (bfs)
- 手机端rem 用法
- 金蝶特性配置(超级BOM)
- 20155318 《网络攻防》Exp5 MSF基础应用
- golang 实现海明距离 demo
- SICP-练习2.17