Click here to Skip to main content
15,904,416 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi, i'm new in C# programming, so please forgive me if this is stupid question but i've searching on google and i can't find the answer.
My issue is that i want to use an array in this way:
myArray["Name"] = "Some Name";
and then get the value like this:
varName = myArray["Name"];

I used to do that in PHP, but in C# don't.
Can anybody give me a hand on this?

Thanks in advance.
Best regards.
Posted

you can use dictionary. Here, is an example :

using System.Collections.Generic;

Dictionary<string,> temp = new Dictionary<string,>();

if (temp.ContainsKey("Name"))
temp.Remove("Name");

temp["Name"] = "Some Name";
MessageBox.Show(temp["Name"]);
 
Share this answer
 
Comments
Pablinff 27-Sep-10 20:45pm    
Reason for my vote of 5
Automatic vote of 5 for accepting answer.
This is not possible.
Arrays can't be used like this.
If you want to index by string instead of number, you have to use something else:
1. An indexer (public string this[string key] { get; set; });
2. A Dictionary<string,>;
3. A StringDictionary;
4. A HashTable;
5. Your own implementation.
 
Share this answer
 
Comments
Pablinff 27-Sep-10 20:46pm    
Reason for my vote of 5
Automatic vote of 5 for accepting answer.
You could do it that way, if you're willing to create a custom collection class. It would probably be easier (and faster) to use a Dictionary, though.
 
Share this answer
 
Comments
Pablinff 27-Sep-10 20:45pm    
Reason for my vote of 5
Automatic vote of 5 for accepting answer.
Hi,

you have various kinds of array in c#. you can reach them in system.collection name space. for what you wan to do, you can use Hashtable or Dictionary<type, type >. note that the key ( name ) should be unique.
 
Share this answer
 
v2
Comments
Pablinff 27-Sep-10 20:45pm    
Reason for my vote of 5
Automatic vote of 5 for accepting answer.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900