是否有"记录"在C#中? [英] Are there "Records" in C#?

查看:134
本文介绍了是否有"记录"在C#中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在寻找存放一些客户数据在内存中,我想这样做是使用一组记录的最佳方式。我不知道这是它在C#什么叫,但基本上我就能够调用客户(我).Name点并有客户名称作为字符串返回。在图灵,其做过这样的:

I'm looking to store some customer data in memory, and I figure the best way to do that would be to use an array of records. I'm not sure if that's what its called in C#, but basically I would be able to call Customer(i).Name and have the customers name returned as a string. In turing, its done like this:

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

我已经搜查,但我似乎无法找到一个相当于C#。可能有人点我在正确的方向?

I've searched, but I can't seem to find an equivalent for C#. Could someone point me in the right direction?

谢谢! :)

推荐答案

好了,好了这将是一个在C#中定义的,所以它可能是这样的:

Okay, well that would be defined in a class in C#, so it might look like this:

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; }
}

然后,你可以有一个名单,其中,T> 那些:

var customers = new List<Customer>();

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

然后你可以,如果你想通过索引来访问这些:

and then you could access those by index if you wanted:

var name = customers[i].Name;

更新: psibernetic <为说/ A>,在 记录 类F#提供了实地平等出了大门,而不是引用平等。这是一个非常重要的区别。为了获得在C#中同样平等的操作,你就需要让这个 A 结构,然后需要生产经营者是否相等;一个很好的例子,被发现是在这个问题上<一个答案href="http://stackoverflow.com/questions/1502451/what-needs-to-be-overriden-in-a-struct-to-ensure-equality-operates-properly">What需要在一个结构,以确保平等被重写可以正常运行?。

UPDATE: as stated by psibernetic, the Record class in F# provides field level equality out of the gate rather than referential equality. This is a very important distinction. To get that same equality operation in C# you'd need to make this class a struct and then produce the operators necessary for equality; a great example is found as an answer on this question What needs to be overriden in a struct to ensure equality operates properly?.

这篇关于是否有&QUOT;记录&QUOT;在C#中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆