c# - 如何声明 C# 记录类型?

标签 c# language-features c#-7.0

我读了on a blog C# 7 将以记录类型为特色

class studentInfo(string StudentFName, string StudentMName, string StudentLName);

但是当我尝试的时候,我得到了这些错误

CS0116 A namespace cannot directly contain members such as fields or methods
CS1022 Type or namespace definition, or end-of-file expected
CS1514 { expected

这应该如何工作?

更新:这是 C# 9 的一个特性 https://devblogs.microsoft.com/dotnet/welcome-to-c-9-0/

最佳答案

更新:

C# 9 now contains record types .

public record Person
{
    public string LastName { get; }
    public string FirstName { get; }

    public Person(string first, string last) => (FirstName, LastName) = (first, last);
}

旧答案:

记录类型(尚未)在 C# 中实现。请参阅官方 GitHub 存储库中的提案:

https://github.com/dotnet/csharplang/blob/master/proposals/records.md

https://github.com/dotnet/csharplang/issues/39讨论或投票

关于c# - 如何声明 C# 记录类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43105895/

相关文章:

c# - C# 字典的原子 AddOrUpdate

c# - .NET ListView 行填充

c# - 基本 C# 语法问题

python - Python 中的技术术语词典?

python - 是否有相当于 `perl -pi -e` 的 Python ?

c# - 为什么在混合 C# ValueTuple 和动态时出现此编译器错误

c# - XPath 和属性

multidimensional-array - Xtend 和数组

c# - ValueTuple 在带有 MVC 5 的 Razor View 中不起作用

asp.net - C# 7 功能在 Visual Studio 2017 RC 上的 Web 项目中不起作用