参了网上资料,原来它是C# 6.0的语法糖。
C# 6.0 新加上的功能:
 

Null-Conditional Operator

大概就是,简洁代码量,缩短一些关于为null的判断~
旧写法:
public static string Truncata(string value, int length)
{
string result = value;
if (value != null)
{
result = value.Substring(, Math.Min(value.Length, length));
}
return result;
}
新的写法:
   public static string Truncata(string value, int length)
{
return value?.Substring(, Math.Min(value.Length, length));
}
我最喜欢的写法: return test?.count ?? 0;

Auto-Property Initializers

大概就是,以往自動屬性初始化無法直接指定,必須要寫在建構函式,現在可以直接指定狀態
旧的
public static string name{get;set;}
static void Main(string[] args)
{
name="test";
}
新的
public static string name{get;set;} ="test";
Nameof Expressions
 用于获取变量、类型或成员的简单(非限定)字符串名称。可以在错误消息中使用类型或成员的非限定字符串名称,而无需对字符串进行硬编码,这样也方便重构。
 
void ThrowArgumentNullExceptionUsingNameof(string parame1)
{
throw new ArgumentNullException(nameof(parame1));
}

Primary Constructors

只读属性可以写成 public string name{get;}="test"。
旧版
public static string name{get;private set;}
新版

public static string name {get;}="test";//不用带上 private set;

Expression Bodied Functions and Properties

大概就是,讲lambda也加在方法里面
旧版
public class class1
{
public string First { get; set; }
public string Second { get; set; }
public override string ToString()
{
return string.Format("{0},{1}", First, Second);
}
}
 
新版:
public override string ToString() => string.Format("{0},{1}", First, Second);

$

可以在字符串里面写明需要赋值的参数。也可以在里面有 ? : 运算

  static void Main(string[] args)
{
Console.WriteLine($"{args[0]},{args[1]}");
}

Static Using Statements

可以将一些静态的方法,写到Using~
using static System.Console;
static void Main(string[] args)
{
WriteLine(“test”);
}

Declaration Expressions

var cppHelloWorld = new Dictionary<int, string>
{
[10] = "main{",
[20] = " printf(\"hello,world\")",
[30] = "}"
};

Exception-Handling Improvements

catch()  when ...
await 
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.ComponentModel;
using System.Runtime.InteropServices;
// ...
[TestMethod][ExpectedException(typeof(Win32Exception))]
public void ExceptionFilter_DontCatchAsNativeErrorCodeIsNot42()
{
try
{
throw new Win32Exception(Marshal.GetLastWin32Error());
}
catch (Win32Exception exception)
if (exception.NativeErrorCode == 0x00042)
{
// Only provided for elucidation (not required).
Assert.Fail("No catch expected.");
}
}

  

try
{
WebRequest webRequest =
WebRequest.Create("http://IntelliTect.com");
WebResponse response =
await webRequest.GetResponseAsync();
// ...
}
catch (WebException exception)
{
await WriteErrorToLog(exception);
}
参考:What's with the dollar sign ($“string”) [duplicate] https://stackoverflow.com/questions/32878549/whats-with-the-dollar-sign-string?rq=1
 
 
 
 

看到Console.WriteLine($"string")写法,一时间不理解$的用途的更多相关文章

  1. C# String 字符串一些关键理解

    #1 :在.Net Framework中,字符总是表示成16位Unicode的代码#2 :String 和string 其实是一样的只是表现形式上不同#3 :string类型被视为基元类型,也就是编译 ...

  2. 第一个输出程序 Console.WriteLine

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  3. 2015.3.12Arinc424 Tools中SiniArincCls.csParserFile(string sFile)函数正则表达式理解

    原文: string RegEx1 = @"(\[ITEM\]\s*=[\S\s]*?(?=\[ITEM\])|\[ITEM\]\s*=[\S\s]*)";//用来识别主记录和后续 ...

  4. 浅析System.Console.WriteLine()

    浅析System.Console.WriteLine() Writeline()函数的功能向 StreamWriter 类写入指定字符串和一行字符,共有19个重载,其与Write()函数的主要区别在于 ...

  5. C#入门——Console.Write()与Console.WriteLine()

    参考:https://blog.csdn.net/qujunyao/article/details/72884670 两者区别: Console.Write("abc"); 输出到 ...

  6. string 常量池的理解

    1: String a="123"; String b="12"+"3"; String c="1"+"23& ...

  7. VS2015使用技巧 为什么我们可以输入cw后按两下tab键出现console.writeline

    镇场诗: 大梦谁觉,水月中建博客.百千磨难,才知世事无常. 今持佛语,技术无量愿学.愿尽所学,铸一良心博客.------------------------------------------ 为什么 ...

  8. 在浏览器控制台输出内容 console.log(string);

    在浏览器控制台中写如数据 1添加    <script type="text/javascript">djConfig = { isDebug: true };< ...

  9. java面试之String的理解(自我理解)

    1.String是基本数据类型吗? 不是,是对象,引用数据类型 2.String是可变吗? 不可变,String是final类型的. 3.怎样比较两个字符串的值相同,怎样比较两个字符串是否为同一对象? ...

随机推荐

  1. 树莓派 连接 JY901(MPU9250) python 代码

    先说BUG,最近要做项目需要树莓派和陀螺仪,资金充足的话肯定是买一个硬件卡尔曼滤波的传感器类似JY901模块,资金不足的就买MPU6050. 网上关于MPU6050在树莓派上的代码还能用,关于JY90 ...

  2. 【jenkins】jenkins+maven+gitlab+testng,jenkins配置

    电脑版本:windows10企业版 jenkins配置: 1.general配置,这里的配置比较简单,基本默认就可以了 2.源码管理 2.1填写git地址,从你的gitlib项目里去找.不会的自行百度 ...

  3. RuntimeError: Model class app_anme.models.User doesn't declare an explicit app_label and isn't in an application in INSTALLED_APPS.---python学习错误记录

    untimeError: Model class app_anme.models.User doesn't declare an explicit app_label and isn't in an ...

  4. bzoj1688

    题解: 暴力枚举生哪一些病 然后判断一下即可 代码: #include<bits/stdc++.h> using namespace std; ,D=; int d,k,n,a[N][D] ...

  5. LCT模板(无讲解)

    怎么说呢,照着打一遍就自然理解了,再打一遍就会背了,再打一遍就会推了. // luogu-judger-enable-o2 #include<bits/stdc++.h> using na ...

  6. 【转载】用实例给新手讲解RSA加密算法

    实践文章:https://mp.weixin.qq.com/s/dCQ17NKWu5ISc-eNhFDlvw 原文地址:http://bank.hexun.com/2009-06-24/1189585 ...

  7. python pexpect包的一些用法

    转自:https://www.jianshu.com/p/cfd163200d12 mark一下,原文中写的挺详细

  8. mpeg2文件分析(纯c解析代码)

    参考链接: 1. MPEG-2码流结构分析 https://www.cnblogs.com/CoderTian/p/9246225.html(本文语法采用这里的截图,代码原创) 1. mpeg2的码流 ...

  9. C++中_T的用途

    1    C++语言中“_T”是什么意思? Visual C++里边定义字符串的时候,用_T来保证兼容性,VC支持ascii和unicode两种字符类型,用_T可以保证从ascii编码类型转换到uni ...

  10. 【转载】IL指令集

    转载自:http://www.cnblogs.com/knowledgesea/p/5461040.html 名称 说明 Add 将两个值相加并将结果推送到计算堆栈上. Add.Ovf 将两个整数相加 ...