I want to create two dimension array of different type like I can add to that array two values one of them is controlname and second is boolean value.
-
I'm not sure I understand your question, but won't a Dictionary work for what you trying to do? Are you trying to store Key/Value pairs? – monkey_p Dec 21 '09 at 10:58
-
Using a list with tuples `public static List
> pixelsArr = new List – vinsa May 28 '18 at 21:28>();`
9 Answers
You can't do that. Instead, you should create a class that contains these two properties, then you can create an array of that type:
public class MyClass
{
public string ControlName {get;set;}
public bool MyBooleanValue {get;set;}
}
public MyClass[] myValues=new MyClass[numberOfItems];
Or, as Anders says, use a dictionary if one of the properties is meant to be used to perform lookups.

- 49,681
- 17
- 108
- 138
A dictionary will work for what you are trying to do then.
Dictionary<string, bool> controllerDictionary = new Dictionary<string, bool>();
To set a value
if (controllerDictionary.ContainsKey(controllerName))
controllerDictionary[controllerName] = newValue;
else
controllerDictionary.Add(controllerName, newValue);
To get a value
if (controllerDictionary.ContainsKey(controllerName))
return controllerDictionary[controllerName];
else
//return default or throw exception

- 2,849
- 2
- 19
- 16
-
You can just use `controllerDictionary[controllerName] = newValue;`, the `ContainsKey` and `Add` aren't required – Sander Rijken Dec 21 '09 at 12:29
-
You can't to that with an array.
Perhaps you should be using a Dictionary?
A generic dictionary of Dictionary<string,bool>
appears to be the kind of thing that will work for your description.

- 489,969
- 99
- 883
- 1,009
It depends on how you want to use your array. Do you want to look up the value by a key, or by its index? Konamiman suggested a class. But a class with two types is nothing more than a Dictionary<type of key, type of value>
.
You can use a dictionary if you want to obtain the value by a key.
Like so:
Dictionary<string, int> MyDict = new Dictionary<string, int>();
MyDict.Add("Brutus", 16);
MyDict.Add("Angelina", 22);
int AgeOfAngelina = MyDict["Angelina"];
Now the disadvantage of a dictionary is that, you can't iterate over it. The order is undetermined. You can not use MyDict[0].Value
to obtain the age of Brutus (which is 16).
You could use a
List<KeyValuePair<string, int>> MyList = new List<KeyValuePair<string, int>>();
to iterate through a 2D array of two different types as a List
supports iteration. But then again, you cannot obtain the age of Angelina by MyList["Angelina"].Value
but you would have to use MyList[0].Value
.
But you could use a datatable as well. But it requires somewhat more work to initialize the table with its columns.

- 11,906
- 8
- 54
- 76
Another way of doing it is to create an array of type object, and then add this to an arraylist. Here is some sample code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
using System.Collections.Generic;
namespace Collections
{
class Program
{
static void Main(string[] args)
{
ArrayList ar = new ArrayList();
object[] o = new object[3];
// Add 10 items to arraylist
for (int i = 0; i < 10; i++)
{
// Create some sample data to add to array of objects of different types.
Random r = new Random();
o[0] = r.Next(1, 100);
o[1] = "a" + r.Next(1,100).ToString();
o[2] = r.Next(1,100);
ar.Add(o);
}
}
}
}

- 41
- 5
If you want to lookup/set a boolean by control name, you could use a Dictionary<string, bool>
.

- 10,724
- 2
- 33
- 50
Use Dictionary<string,bool>. If, for some reason, you really need an array, try object[,] and cast its values to the types you want.

- 11
- 1
"A multi-dimensional array is an array: all elementsin all dimensions have the same type"

- 1,516
- 1
- 18
- 30
Actually you can do it with a List. I suggest you take a look at Tuples.
How to easily initialize a list of Tuples?
You can use a list of type object too if you want
Like so :List<List<object>> Dates = new List<List<object>>() { };
Dates.Add(new List<object>() { 0 ,"January 1st" });

- 21
- 1
- 5