Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the easiest way to handle associative array in c#?

I do not have a lot of experience with C#, yet I am used of working with associative arrays in PHP.

I see that in C# the List class and the Array are available, but I would like to associate some string keys.

What is the easiest way to handle this?

Thx!

like image 852
Michael Avatar asked Apr 20 '12 16:04

Michael


People also ask

Which is best looping technique that can be used in accessing associative array elements?

A for loop can be used to access every element of an array.

What is are the ways for declaring associative array?

There are two ways to loop around the associative array. First, by using the for loop, and then by using the 'foreach' command. Example: In Associative arrays in PHP, the array keys() function is used to find indices with names provided to them, and the count() function is used to count the number of indices.

What is associative array in C?

Associative arrays in C++ C++Server Side ProgrammingProgramming. In c++ programming language, an associative array is a special type of array in which the index value can be of any data type i.e. it can be char, float, string, etc. These associative arrays are also known as maps or dictionaries.

What can be used in associative array?

You can use any type with an associative array key or value that you can use with a scalar variable. You cannot nest an associative array within another associative array as a key or value. You can reference an associative array using any tuple that is compatible with the array key signature.


1 Answers

Use the Dictionary class. It should do what you need. Reference is here.

So you can do something like this:

IDictionary<string, int> dict = new Dictionary<string, int>(); dict["red"] = 10; dict["blue"] = 20; 
like image 102
dcp Avatar answered Oct 07 '22 18:10

dcp