c# - C#中有 "Records"吗?

标签 c# .net visual-studio-2012 record

我想在内存中存储一​​些客户数据,我认为最好的方法是使用记录数组。我不确定这是否是它在 C# 中的调用方式,但基本上我可以调用 Customer(i).Name 并将客户名称作为字符串返回。在图灵中,它是这样完成的:

type customers :
    record
        ID : string
        Name, Address, Phone, Cell, Email : string
        //Etc...
    end record

我搜索过,但似乎找不到 C# 的等效项。有人能指出我正确的方向吗?

谢谢! :)

最佳答案

好的,那将在 class 中定义在 C# 中,所以它可能看起来像这样:

public class Customer
{
    public string ID { get; set; }
    public string Name { get; set; }
    public string Address { get; set; }
    public string Phone { get; set; }
    public string Cell { get; set; }
    public string Email { get; set; }
}

然后你可以有一个List<T>其中:

var customers = new List<Customer>();

customers.Add(new Customer
{
    ID = "Some value",
    Name = "Some value",
    ...
});

然后您可以根据需要通过索引访问它们:

var name = customers[i].Name;

更新:如 psibernetic 所述, Record F# 中的类提供了字段级别的相等性,而不是引用相等性。这是一个非常重要的区别。要在 C# 中获得相同的相等操作,您需要制作此 class一个struct然后产生相等所必需的运算符;找到一个很好的例子作为这个问题的答案 What needs to be overridden in a struct to ensure equality operates properly? .

关于c# - C#中有 "Records"吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19280807/

相关文章:

c# - ServiceReferences.ClientConfig 中的动态端点

c# - DataTable——如何实现数据列表达式(计算列)的持久化?

c# - Razor 引擎的任何好的替代品

c# - asp.net mvc 2.0如何让页面自动使用https

c# - 是否可以在仅使用 .NET 或其他一些内置 Windows API 维护层次结构的同时解压缩 zip 文件?

c# - 无法关闭虚拟键盘

.net - 使用 ViewModel 进行 UI 线程化

c# - ReSharper 单元测试未在 bin 目录中运行

c# - 模糊图像的选定部分

c# - 如何在我的 ASP.NET MVC 5 项目中获取 “Add Controller” 和 “Add View” 菜单选项?