DELETE me
DELETE me

Reputation:

are this way really good in c# for passing parameter

In my apps i need a List to pass as a parameter.

i write it

List<stuff.Inventory> inv = new List<stuff.Inventory>();
inv.Add(stuff.Inventory.NotAvailable);
inv.Add(stuff.Inventory.None);
inv.Add(stuff.Inventory.Coming);

if (stuff.isStuffinInv(inv, prodID))
{
    // ...
}

are my way is right or need any new way to do this

Upvotes: 2

Views: 155

Answers (4)

Jon Skeet
Jon Skeet

Reputation: 1500585

Well, aside from naming conventions and indentation, it looks okay.

With C# 3 you can use a collection initializer, like this:

List<Inventory> inventory = new List<Inventory>
{
    Inventory.NotAvailable, Inventory.None, Inventory.Coming
};
if (stuff.isStuffInInv(inventory, prodID))
...

That's a generally more readable approach, IMO.

Upvotes: 6

Martin Clarke
Martin Clarke

Reputation: 5657

List<stuff.Inventory> inv = new List<stuff.Inventory>()
{
   stuff.Inventory.NotAvailable, 
   stuff.Inventory.None, 
   stuff.Inventory.Coming
};

From what you've described though, would an enum be more suitable?

Upvotes: 2

Mark H
Mark H

Reputation: 13897

There's nothing particularly wrong with that code, but you might want to consider using LINQ to query your list, rather than having your isStuffInInv method.

if (inv.Any(x => x._?_ == prodID));

Upvotes: 0

James
James

Reputation: 82096

From the looks of it Inventory appears to be an emum. Have you considered using the FlagsAttribute instead?

Upvotes: 5

Related Questions