Defining two dimensional Dynamic Array with different types
I want to create two dimension array of different type like I can add to that array two values开发者_JS百科 one of them is controlname and second is boolean value.
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.
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
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.
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.
If you want to lookup/set a boolean by control name, you could use a Dictionary<string, bool>
.
Use Dictionary<string,bool>. If, for some reason, you really need an array, try object[,] and cast its values to the types you want.
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);
}
}
}
}
"A multi-dimensional array is an array: all elementsin all dimensions have the same type"
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" });
精彩评论