我发现的这个奇怪的 C# class 构造函数语法是什么?
What is this strange C# class constructor syntax I've found?
我下载了 Jon Skeet called 'PclPal' as found here 的演示 - 由于某些我不认识的 C# 语法,它拒绝在 Visual Studio 2013 中编译。
两个 .cs 文件似乎在实际 class 声明中具有构造函数逻辑,如下所示:
public class Profile(string path, IEnumerable<SupportedRuntime> runtimes)
{
public string Name { get; } = System.IO.Path.GetFileName(path);
public String Path { get; } = path;
public IReadOnlyCollection<SupportedRuntime> SupportedRuntimes { get; } = runtimes.ToList().AsReadOnly();
这是什么?这是我还没有学过的 C# 的未来版本吗?
编辑:我不认为这是一个重复的问题。链接的问题是“为什么主构造函数不编译?”,而这个问题实际上是说 'What are [these things that turn out to be] primary constructors?'
这些是最初出现在 C# 6 中的功能,"Auto Properties with Initializers" 和 "Primary Constructors"。
我下载了 Jon Skeet called 'PclPal' as found here 的演示 - 由于某些我不认识的 C# 语法,它拒绝在 Visual Studio 2013 中编译。
两个 .cs 文件似乎在实际 class 声明中具有构造函数逻辑,如下所示:
public class Profile(string path, IEnumerable<SupportedRuntime> runtimes)
{
public string Name { get; } = System.IO.Path.GetFileName(path);
public String Path { get; } = path;
public IReadOnlyCollection<SupportedRuntime> SupportedRuntimes { get; } = runtimes.ToList().AsReadOnly();
这是什么?这是我还没有学过的 C# 的未来版本吗?
编辑:我不认为这是一个重复的问题。链接的问题是“为什么主构造函数不编译?”,而这个问题实际上是说 'What are [these things that turn out to be] primary constructors?'
这些是最初出现在 C# 6 中的功能,"Auto Properties with Initializers" 和 "Primary Constructors"。