看到Console.WriteLine($"string")写法,一时间不理解$的用途

参了网上资料,原来它是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(0, Math.Min(value.Length, length));
            }
            return result;
        }
新的写法:
   public static string Truncata(string value, int length)
        {
            return value?.Substring(0, 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
C# : The New and Improved C# 6.0  https://msdn.microsoft.com/en-us/magazine/dn802602.aspx
 
 
 
 

猜你喜欢

转载自www.cnblogs.com/nanguoyezi/p/9387877.html