4

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

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

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

谢谢!:)

4

4 回答 4

8

好的,这将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,F# 中的Record类提供了门外的字段级别相等性,而不是引用相等性。这是一个非常重要的区别。要在 C# 中获得相同的相等操作,您需要将其设为classa struct,然后生成相等所需的运算符;找到了一个很好的例子作为这个问题的答案什么需要在结构中被覆盖以确保相等性正常运行?.

于 2013-10-09T19:26:45.010 回答
2

一个类或一个结构可以在这里工作。

    class Customer
    {
        string Name
        {
            get;
            set;
        }
        string Email
        {
            get;
            set;
        }
    }

    Customer[] customers = new Customer[50];
    //after initializing the array elements, you could do
    //assuming a for loop with i as index
    Customer currentCustomer = customers[i];
    currentCustomer.Name = "This";
于 2013-10-09T19:27:33.417 回答
1

It appears that the "type" you are looking for is actually a Class.

class Customer {
  string id, name, phone, cell, email;
}

List<Customer> customerList = new List<Customer>();

Check this link for more detail on classes... you may want to do a bit of research, reading and learning :-)

http://msdn.microsoft.com/en-us/library/vstudio/x9afc042.aspx

于 2013-10-09T19:25:33.013 回答
0

Assuming you have a class which models your customers, you can simply use a List of customers.

var c = new List<Customers>()

string name = c[i].Name
于 2013-10-09T19:26:04.813 回答