当前位置: 移动技术网 > IT编程>开发语言>c# > C# 6.0 新特性汇总

C# 6.0 新特性汇总

2019年07月18日  | 移动技术网IT编程  | 我要评论

1. 静态using(static using)

静态using声明允许不使用类名直接调用静态方法。

the static using declaration allows invoking static methods without the class
name.
in c# 5
using system;
console.writeline("hello, world!");
in c# 6
using static system.console;
writeline("hello, world");

2. 表达式方法(expression-bodied methods)

使用表达式方法,只有一条语句的方法可以使用lambda语法写。

with expression-bodied methods, a method that includes just one statement can
be written with the lambda syntax.
in c# 5
public bool issquare(rectangle rect)
{
return rect.height == rect.width;
}
in c# 6
public bool issquare(rectangle rect) => rect.height == rect.width;

3. 表达式属性(expression-bodied properties)

跟表达式方法类似,只有一个get访问器的单行属性可以使用lambda语法写。

similar to expression-bodied methods, one-line properties with only a get accessor
can be written with the lambda syntax
in c# 5
public string fullname
{
get
{
return firstname +"" + lastname;
}
}
in c# 6
public string fullname => firstname +"" + lastname;

4. 自动属性初始化器(auto-implemented property intializers)

自动属性可以使用属性初始化器初始化。

auto-implemented properties can be initialized with a property initializer.

in c# 5
public class person
{
public person()
{
age = 24;
}
public int age {get; set;}
}
in c# 6
public class person
{
public int age {get; set;} = 42;
}

5. 只读自动属性(read-only auto properties)

c# 5需要完整的属性语法实现只读属性,c# 6可以使用自动属性实现。

to implement read-only properties, c# 5 requires the full property syntax. with
c# 6, you can do this using auto-implemented properties.
in c# 5
private readonly int _bookid;
public bookid
{
get
{
return _bookid;
}
}
in c# 6
public bookid {get;}

6. nameof操作符(nameof operator)

字段、属性、方法和类型的name可以通过nameof访问。使用nameof,可以方便的重构name变化。

with the new nameof operator, names of fields, properties, methods, or types can
be accessed. with this, name changes are not missed with refactoring.

in c# 5
public void method(object o)
{
if (o == null) throw new argumentnullexception("o");
in c# 6
public void method(object o)
{
if (o == null) throw new argumentnullexception(nameof(o));

7. null传递操作符(null propagation operator)

null传递操作符简化了空值检查。

the null propagation operator simplifies null checks.
in c# 5
int? age = p == null ? null : p.age;
var handler = event;
if (handler != null)
{
handler(source, e);
}
in c# 6
int? age = p?.age;
handler?.invoke(source, e);

8. 字符串插值(string interpolation)

字符串差值移除了对string.format的调用,使用表达式占位符取代数字格式占位符。

the string interpolation removes calls to string.format. instead of using
numbered format placeholders in the string, the placeholders can include
expressions.
in c# 5
public override tostring()
{
return string.format("{0}, {1}", title, publisher);
}
in c# 6
public override tostring() => $"{title} {publisher}";

9. 字典初始化器(dictionary initializers)

字典可以使用类似集合的字典初始化器初始化。

dictionaries can now be initialized with a dictionary initializer—similar to the
collection initializer.
in c# 5
var dict = new dictionary<int, string>();
dict.add(3,"three");
dict.add(7,"seven");
in c# 6
var dict = new dictionary<int, string>()
{
[3] ="three",
[7] ="seven"
};

10. 异常过滤器(exception filters)

异常过滤器允许你在捕获异常前进行过滤。

exception filters allow you to filter exceptions before catching them.

in c# 5
try
{
//etc.
} catch (myexception ex)
{
if (ex.errorcode != 405) throw;
// etc.
}
in c# 6
try
{
//etc.
} catch (myexception ex) when (ex.errorcode == 405)
{
// etc.
}

11. 在catch使用await(await in catch)

await可以在catch块中直接使用,c# 5中需要变通使用。

await can now be used in the catch clause. c# 5 required a workaround.
in c# 5
bool haserror = false;
string errormessage = null;
try
{
//etc.
} catch (myexception ex)
{
haserror = true;
errormessage = ex.message;
} 
if (haserror)
{
await new messagedialog().showasync(errormessage);
}
in c# 6
try
{
//etc.
} catch (myexception ex)
{
await new messagedialog().showasync(ex.message);
}

以上所述是小编给大家介绍的c# 6.0 新特性汇总,希望对大家有所帮助

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网