[Unity脚本运行时更新]C#7.1新特性

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zhenghongzhi6/article/details/83010669

洪流学堂,让你快人几步!本文首发于洪流学堂微信公众号。

本文是该系列《Unity脚本运行时更新带来了什么?》的第6篇。
洪流学堂公众号回复runtime,获取本系列所有文章。

Unity2017-2018.2中的4.x运行时已经支持到C#6,之前的文章已经介绍完毕。Unity2018.3将支持到C# 7.3,今天我们先来看看C#7.1新特性能给代码带来什么吧,不过这些特性得等到Unity2018.3才可以用哦

C#7.1 新特性

C#7.1是第一次以次要更新的方式发布C#语言的更新,可以让开发者更快的使用新功能。

默认字面值表达式

默认字面值表达式可以简化默认值表达式。比如之前你可能需要这么写:

Func<string, bool> whereClause = default(Func<string, bool>);

现在你可以省略右边的类型:

Func<string, bool> whereClause = default;

更多关于默认值的增强,可以查看相关文档:
https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/statements-expressions-operators/default-value-expressions

推断元组元素名称

此功能是对C#7.0中引入的元组功能的一个小改进。很多时候初始化元组时,右侧用于赋值的变量与元组元素的名称相同:

int count = 5;
string label = "Colors used in the map";
var pair = (count: count, label: label);

在C# 7.1中,元组元素的名称可以从用于初始化元组的变量中推断出来:

int count = 5;
string label = "Colors used in the map";
var pair = (count, label); // element names are "count" and "label"

Async main 异步Main方法

这个功能与Unity内的脚本编程没有太大关系,不过如果你也开发C#应用的话,也可以了解一下

在async main方法种,你可以使用await
之前你需要这么写:

static int Main()
{
    return DoAsyncWork().GetAwaiter().GetResult();
}

现在你可以这么写:

static async Task<int> Main()
{
    // This could also be replaced with the body
    // DoAsyncWork, including its await expressions:
    return await DoAsyncWork();
}

如果Main没有返回值,可以直接返回Task类型:

static async Task Main()
{
    await SomeAsyncMethod();
}

小结

本文讲解了C#7.1的新特性中对Unity编程有影响的新特性,不过这些特性得等到Unity2018.3才可以用哦

洪流学堂公众号回复runtime,获取本系列所有文章。

把今天的内容分享给其他Unity开发者朋友,或许你能帮到他。



《郑洪智的Unity2018课》,倾尽我8年的开发经验,结合最新的Unity2018,带你从入门到精通。

猜你喜欢

转载自blog.csdn.net/zhenghongzhi6/article/details/83010669