参了网上资料,原来它是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. 下面程序的输出结果是____ A:11,10 B:11,11 C:10,10 D:10,11 int x=10; int y=x++; printf("%d,%d",(x++,y),y++);

    下面程序的输出结果是____ A:11,10 B:11,11 C:10,10 D:10,11 int x=10; int y=x++; printf("%d,%d",(x++,y) ...

  2. 和初学者交流学习java语言一些体会。

    对初学者,自己学习java语言的一些体会: 1.工欲善其事,必先利其器.先把Java学习工具准备好,包括集成开发环境IDE,自己觉得MyEclipse比较适合,还有数据库,Oracle或MySQL,S ...

  3. 有效的括号序列——算法面试刷题4(for google),考察stack

    给定一个字符串所表示的括号序列,包含以下字符: '(', ')', '{', '}', '[' and ']', 判定是否是有效的括号序列. 括号必须依照 "()" 顺序表示, & ...

  4. 简述 JVM 垃圾回收算法

    经典垃圾回收 标记-清除(Mark-Sweep) 研发园开了家新餐厅,餐厅老板在考虑如何回收餐盘时首先使用了最简单的方式,那就是服务员在顾客用餐的过程中,不定时的观察餐厅,针对用完餐的顾客记录他们的位 ...

  5. css3 翻牌动画

    最近做了一个特效,css是从网上找的,地址是这个: CSS3 animate flip下的纸牌翻转效果实例页面 把其中核心的css代码扒出来如下: /* The properties in this ...

  6. java根据输入的字符串和字节数来截取,输出对应字节数的字符串

    public class Test { //要进行截取操作的字符串 static String ss; //截取的字符串的字节数 static int n; public static void ma ...

  7. html中form表单的使用方法和介绍

    from表单的使用方法 一.表单赏析 二.了解表单功能:用于搜集不同类型的用户输入的内容 有了表单,网页的内容可以由用户自己创建,那么对于网页来说,我们既是网页创建都者,也是网页的消费者. 三.常用的 ...

  8. shutil

    import shutil#功能是将db文件中的内容拷贝到haha这个文件中去,但是haha原有的内容会被清空#shutil.copyfileobj(open('db','r',encoding='u ...

  9. HFun.快速开发平台(三)=》通用系统用户选择

    系统中用户的选择使用特别多,将该功能统一实现可提升系统效率. 用户的选择实现相对简单,系统中基本都会提供,HFun.快速开发平台中的实现特点主要有: 1.用户的选择分为单用户选择和多用户选择. 2.用 ...

  10. DAY1 练习

    要求:⽤户登陆(三次输错机会)且每次输错误时显示剩余错误次数(提示:使⽤字符串格式化), 如果三次用完了之后 问是否再试试 再给三次机会 如果不想试了说没有机会了. list = [{'usernam ...