Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are there "Records" in C#?

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

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

Thanks! :)

like image 463
Nathan Avatar asked Dec 20 '22 23:12

Nathan


1 Answers

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

Then you could have a List<T> of those:

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;

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 overridden in a struct to ensure equality operates properly?.

like image 68
Mike Perrenoud Avatar answered Dec 23 '22 12:12

Mike Perrenoud