Reputation: 11496
I need to implement in C# the following structure from ActionScript3:
objectArray.push({name:"Stx10",category:123 , isSet:false, isEmpty:true});
So that later I can access the objects from the array like this:
String name =objectArray[i].name
So I naturally though about C Sharp Hash Tables but the allow only a single Key -> Value insertion. I can't believe .NET framework has not solution for such a thing... Any help will be greatly appreciated!
Upvotes: 0
Views: 4508
Reputation: 54011
It looks to me like you're pushing a custom type to an array.
Using an IList will give you a quick Add method in which you can pass a new object of you type such as:
IList<MyType> myCollection = new List<MyType>();
myCollection.Add(new MyType{
Name = "foo",
Category = "bar",
IsSrt = true,
IsEmpty = true
});
UPDATE
Just to add a bit of extra value based on Henk's comment on Porges's answer, here's a way to do the same thing using a dynamic type and thus removing the need for a custom type:
IList<dynamic> myCollection = new List<dynamic>();
myCollection.Add(new {
Name = "foo",
Category = "bar",
IsSet = true,
IsEmpty = true});
Upvotes: 2
Reputation: 9019
Don't forget about
var d=new Dictionary<string, object>();
What's your intention though?
Upvotes: 0
Reputation: 30590
If you're just accessing elements by index as in your example, then you don't need hashed access, you can just use a List<T>
.
I'd encapsulate your information into a type like this:
class Thing {
public string Name {get; set;}
public int Category {get; set;}
public bool IsSet {get; set;}
public bool IsEmpty {get; set;}
}
Then:
objectList.Add(new Thing{Name="Stx10", Category=123, IsSet=false, IsEmpty=true})
// ...
string name = objectList[i].Name;
Upvotes: 2