当前位置: 移动技术网 > IT编程>开发语言>.net > VS2015中C#版本6.0的新特性 你需要知道

VS2015中C#版本6.0的新特性 你需要知道

2017年12月12日  | 移动技术网IT编程  | 我要评论

r230清零图解,曹可凡 嫖娼,光纤激光打标机

本文列出个人感觉比较有用的几个新功能,供大家参考,具体内容如下 
注意:这些新特性只能用于vs2015及更高版本,无法在vs2013、vs2010等低版本中使用。当然,如果你不喜欢这些新的特性,仍然可以继续使用原来的用法(所以说它是新的语法糖)。
 1、自动属性初始化的改进(有用)
 原来的用法(声明时无法同时初始化),例如:

 class myclass
{
  public int age { get; set; }
  public string name { get; set; }
  public myclass()
  {
    age = 20;
    name = "张三";
  }
} 

新用法(声明时可同时初始化,更方便了),例如:

 class myclass
{
  public int age { get; set; } = 20;
  public string name { get; set; } = "张三";
} 

2、string.format的改进(有用)
 原来的用法:用string.format(…)实现,例如:

class myclass
{
  public void mymethod()
  {
    string name = "张三";
    int age = 20;
    string s1 = string.format("{0},{1}", name, age);
    string s2 = string.format("姓名={0},年龄={1}", name, age);
    string s3 = string.format("{0,15},{1:d3}", name, age);
    string s4 = string.format("{0,15},{1,10:d3}", name, age);
    console.writeline("{0},{1},{2},{3}", s1, s2, s3 ,s4);
    string s5 = string.format("{0:yyyy-mm-dd}", datetime.now);
  }
} 

新用法:用“$”前缀实现(变量直接写到大括号内,而且带智能提示,更方便了),例如: 

class myclass
{
  public void mymethod()
  {
    string name = "张三";
    int age = 20;
    string s1 = $"{name},{age}";
    string s2 = $"姓名={name},年龄={age}";
    string s3 = $"{name,15},{age:d3}";
    string s4 = $"{name,15},{age,10:d3}";
    console.writeline($"{s1},{s2},{s3},{s4}");
    string s5 = $"{datetime.now:yyyy-mm-dd}";
  }
} 

3、字典的初始化
 原来的用法: 

class myclass
{
  public void mymethod()
  {
    dictionary<string, int> student = new dictionary<string, int>();
    student.add("a1", 15);
    student.add("a2", 14);
    student.add("a3", 16);
  }
} 

新用法(可以直接写初始化的值,更方便了): 

class myclass
{
  public void mymethod()
  {
    dictionary<string, int> student = new dictionary<string, int>()
    {
      ["a1"] = 15,
      ["a2"] = 14,
      ["a3"] = 16
    };
  }
} 

4、可以用static声明静态类的引用
 原来的用法: 

using system;
namespace myapp
{
  class demo1new
  {
    public static double mymethod(double x, double angle)
    {
      return math.sin(x) + math.cos(angle);
    }
  }
} 

新用法(表达式比较复杂的时候有用,代码更简洁了):

using static system.math;
namespace myapp
{
  class demo1new
  {
    public static double mymethod(double x, double angle)
    {
      return sin(x) + cos(angle);
    }
  }
} 

5、nameof表达式
 假定wpf应用程序中有下面的类: 

public class myclass
 {
 
public string mytext { get; set; } = "aaa";
 
}

 并假定有下面的xaml代码:
 <stackpanel>
 
<textblock name="txt1"/>
 
……
 
</stackpanel>
 代码隐藏类中原来的用法:
 txt1.setbinding(textblock.textproperty, "mytext");
现在的用法(因为有错误检查智能提示,用起来更方便了):
 txt1.setbinding(textblock.textproperty, nameof(myclass.mytext));
6、null-条件表达式
(有用)

 var ss = new string[] { "foo", null };
var length0 = ss [0]?.length; // 结果为3
var length1 = ss [1]?.length; // 结果为null
var lengths = ss.select (s => s?.length ?? 0); //结果为[3, 0] 

7、在try-catch-finally中使用await
 异步编程中,原来在catch或者finally中无法使用await,现在可以了: 

async void somemethod()
{
  try
  {
    //...etc...
  }
  catch (exception x)
  {
    var diagnosticdata = await generatediagnosticsasync (x);
    logger.log (diagnosticdata);
  }
  finally
  {
    await someobject.finalizeasync();
  }
} 

 8、其他
 c# 6.0还有一些新的特性,对于初学者来说用的不是太多,所以这里就不再介绍了。
 再次说明一下,如果你不喜欢新的特性,仍然可以继续使用原来的用法。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网