gpt4 book ai didi

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

转载 作者:IT王子 更新时间:2023-10-29 04:26:20 26 4
gpt4 key购买 nike

我读了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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com