参了网上资料,原来它是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. Chrome的Copy Selector中带有\3

    F12,右键copy selector就是这个结果. 查询到如下资料: https://stackoverflow.com/questions/20043347/css-css-coding-that ...

  2. SpringBoot入门最详细教程

    monkey01 关注 2017.08.08 13:36* 字数 1479 阅读 34248评论 0喜欢 15 网上有很多springboot的入门教程,自己也因为项目要使用springboot,所以 ...

  3. java Data、String、Long三种日期类型之间的相互转换

    java Data.String.Long三种日期类型之间的相互转换      // date类型转换为String类型   // formatType格式为yyyy-MM-dd HH:mm:ss// ...

  4. String字符串的常用方法

    1.substr():可在字符串中抽取从 start 下标开始的指定数目的字符. stringObject.substr(start,length) start:必需.要抽取的子串的起始下标.必须是数 ...

  5. 学号 20175212 《Java程序设计》第4周学习总结

    学号 20175212 <Java程序设计>第4周学习总结 教材学习内容总结 一. 子类与父类 父类中的private和友好访问权限的成员变量不会被子类继承.子类只继承父类中的protec ...

  6. CentOS7系统上的GPSTK示例代码调试 & 运行结果 & 心得

    下载的源码程序包中,共有16个例子,这里记录它们的调试及运行结果,尤其是哪些可用,哪些不可用,今后使用时用作参考. 总结: (1)在 18 个示例程序中,example16 和 example17 编 ...

  7. Python小札

    在Python中,等号(=)是赋值语句,可以把任意数据类型赋值给变量,同一个变量可以反复赋值,而且可以是不同类型的变量如: a = 123 #a是整数 print(a) a = 'ABC' #a变为字 ...

  8. vs code编写的时候自动回车的原因

    今天在书写一个空白的scss文件的时候,刚打2个字就自动回车换行了: 测试了几次,发现了规律就是输入停下来0.5秒左右就会自动换行,比如,打了2个中文,再连续打一个句子,键盘虽然在动,但是vs cod ...

  9. C语言常见易错题集(分析及解答)(仅可用于交流,勿用于商业用途)

    1.能正确表示a和b同时为正或同时为负的逻辑表达式是( D  ). A.(a>=0||b>=0)&&(a<0||b<0)             B.(a> ...

  10. (一)java异常处理的几个问题

    1.java中两种异常? 答:java中存在两种异常:受检查(checked)异常和不受检查(unchecked)异常.不受检查的异常不需要在方法或者构造函数上声明,就算是方法或是构造函数会发生这样的 ...