3

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!

3 Answers 3

11

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);
Sign up to request clarification or add additional context in comments.

8 Comments

Be proactive and add a string for additional data (special offers, rebates, html links, you name it)
@pe: i could add millions of other things like overriding ToString or Equals+GetHashCode, parsing from string or..or..or. But it would just distract from the essential. I think OP is clever enough to understand the concept.
It was no criticism, rather a suggestion to the OP. Your answer is close to perfect (no kidding).
Brand could be an enum, to avoid spelling mistakes.
@BCdotNET: and if you get a new brand/customer you have to recompile?
|
4

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

Comments

2

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

2 Comments

Structures can also do the job similarly as explained in other answers and list(array) can be used as collection of objects
@AliKazmi Structures as struct can be used, but that will be a poor choice. Phone data type is more like a class in C# terms. Not sure what's your point anyway.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.