Thom
Thom

Reputation: 621

Store data in array, object, struct, list or class. C#

I want to make a program that store phones like:

Brand:    Samsung
Type:     Galaxy S3
Price:    199.95
Ammount:  45
-------------------
Brand:    LG
Type:     Cookie
Price:    65.00
Ammount:  13
-------------------
etc, etc, etc,

What is the best practices to do that?
In php I should have done:

$phones = array(
    array(
        array("Brand"   => "Samsung"),
        array("Type"    => "Galaxy S3"),
        array("Price"   => 199.95),
        array("Ammount" => 45)
    ),
    array(
        array("Brand"   => "LG"),
        array("Type"    => "Cookie"),
        array("Price"   => 65.00),
        array("Ammount" => 13)
    )
)

Is this also possible in C#, because I dont know how many phones there are going in the list, and the datatypes are different: string, decimal, int. I dont know what to use because you have lists, structs, objects, classes and so further.

Thanks in advance!

Upvotes: 3

Views: 21547

Answers (3)

Tim Schmelter
Tim Schmelter

Reputation: 460288

Use a class like:

public class Phone
{
    public string Brand { get; set; }
    public string Type { get; set; }
    public decimal Price { get; set; }
    public int Amount { get; set; }
}

Then you can fill a List<Phone>, for example with the collection initializer syntax:

var phones = new List<Phone> { 
    new Phone{
        Brand = "Samsung", Type ="Galaxy S3", Price=199.95m, Amount=45
    },
    new Phone{
        Brand = "LG", Type ="Cookie", Price=65.00m, Amount=13
    } // etc..
};

... or in a loop with List.Add.

Once you've filled the list you can either loop it to get one phone at a time

For example:

foreach(Phone p in phones)
    Console.WriteLine("Brand:{0}, Type:{1} Price:{2} Amount:{3}", p.Brand,p.Type,p.Price,p.Amount);

or you can use the list indexer to access a specific phone at a given index:

Phone firstPhone = phones[0]; // note that you get an exception if the list is empty

or via LINQ extension methods:

Phone firstPhone = phones.First(); 
Phone lastPhone  = phones.Last(); 
// get total-price of all phones:
decimal totalPrice = phones.Sum(p => p.Price);
// get average-price of all phones:
decimal averagePrice = phones.Average(p => p.Price);

Upvotes: 11

Mateusz Rogulski
Mateusz Rogulski

Reputation: 7455

Best solution is to create your Phone object like:

public class Phone {
    public string Brand { get; set; }
    public string Type { get; set; }
    public decimal Price { get; set; }
    public decimal Ammount { get; set; }
}

and store this object in list (for example):

List<Phone> phones = new List<Phone> ();
phones.Add(new Phone { Brand = "Samsung", Type = "Galaxy S3", Price = 199.95, Amount = 45 });
etc

Upvotes: 4

oleksii
oleksii

Reputation: 35925

You would have a model class, like

class Phone
{
     public string Brand  {get; set;}
     public string Type   {get; set;}
     public decimal Price {get; set;}
     public int Amount    {get; set;}
}

And then to create a list of the phones, you can use code like this

var phones = new List<Phone>
{
    new Phone{Brand = "Samsung", Type = "Galaxy S3", Price = 199.95, Amount = 45},
    new Phone{Brand = "LG",  Type = "Cookie", Price = 65.00, Amount = 13},
}

Upvotes: 2

Related Questions