4

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! :)

Nat
  • 890
  • 3
  • 11
  • 23

4 Answers4

8

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?.

Community
  • 1
  • 1
Mike Perrenoud
  • 66,820
  • 29
  • 157
  • 232
  • `name` is a very strange name for a `Customer` :) – Sriram Sakthivel Oct 09 '13 at 19:28
  • `var name = customers[i];` looks strange naming convention isn't it? `var customer = customers[i];` would be good. – Sriram Sakthivel Oct 09 '13 at 19:30
  • @SriramSakthivel, yeah I meant to end that statement by accessing the `Name` property like the OP did in the question. I saw what you meant afterwards and edited my answer. – Mike Perrenoud Oct 09 '13 at 19:31
  • Thanks, this should work perfectly! Where in my code would I put this though if I want to be able to access the values anywhere in the program? If I put it in form2.cs but set it to public, is it usable by any form? – Nat Oct 09 '13 at 19:32
  • @user2856410, put the class in its own file. The class is `public` so its accessible to the entire assembly as well as consumers of the assembly. – Mike Perrenoud Oct 09 '13 at 19:33
  • Awesome, thanks! I'll have to play around with this for a bit to figure it all out, but it should work just fine. I'll post another question if I have any problems. Thanks guys! :) – Nat Oct 09 '13 at 19:35
  • 1
    @user2856410 Should note that while the class is public, you'd probably want to pass the List of customers from form to form. Otherwise you'd have to make a new List on each form and re-populate it. The customer class is just the "template" to store all of the customer info, the List is what actually has the meat and veggies of the data. – sab669 Oct 09 '13 at 19:57
  • This overlooks what is the refining factor of structure types in F#: structural equality instead of reference. Example: type RecordTest = { X: int; Y: int } let record1 = { X = 1; Y = 2 } let record2 = { X = 1; Y = 2 } record1 and record2 are equal because their contexts are equal, similar to struct in C#, but with implied equality operators. You can do this in C# with a struct, but you will have to define equality and inequality operators. http://msdn.microsoft.com/en-us/library/dd233184.aspx – AIDA Mar 07 '14 at 18:30
  • 1
    @psibernetic: that's a very good addition! I'll update for those reading in the future. – Mike Perrenoud Mar 07 '14 at 18:41
2

A class or a struct would work here.

    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";
Crowe T. Robot
  • 2,045
  • 17
  • 18
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

adam
  • 2,930
  • 7
  • 54
  • 89
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
fvdalcin
  • 1,047
  • 1
  • 8
  • 26