我想在内存中存储一些客户数据,我认为最好的方法是使用记录数组。我不确定这是否是它在 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/