C # Defined Arrays

Can I define an array so that the first element is String, the second is int, and the third is a text field?

As when creating a list, we select the type of the element List<string >

Update from comment :

Sorry, I can’t explain. I need to like this List<string,int,object> First, I will set the type and when I call the list I don’t need to drop

thanks

+6
source share
11 answers

create a list of objects. in C # everything comes from object

 List<object> list = new List<object> {"first", 10, new TextBox()}; 

EDIT (comment):

Then you must create a separate class to store these three elements or use Tuple

 List<Tuple<string,int,TextBox>> list; 
+9
source

You can declare an array of an object and do it. You are talking about an array of mixed type, right?

 var arr = new object[] { "Hi", 42, 3.7, 'A' } 
+6
source

If you need an array with elements without a common base class other than an object, then you need an array of objects!

 object[] myArray = new object[] { "Hi", 23, new TextBox() }; 

Please note that this is not what you should do. If you need to associate disparate types like this, a class makes much more sense.

+4
source

You want a Tuple<string,int,TextBox> , not an array.

+4
source

IMHO the best way to do this is through List<> of objects :

 String s = "hey!"; int i = 156; TextBox t = new TextBox(); List<object> list = new List<object>(3); list.Add(s); list.Add(i); list.Add(t); 

The reason for this is that (almost?) Everything in C # comes from the base class of object

+3
source

Arrays are usually homogeneous collections, which means that each object contains the same type in the array (or at least the generic type of the parent type). The array [string, int, textbox] can be defined as object[] , but this is really the wrong use of arrays.

Just create the correct class that contains 3 fields.

 class MyType { public string myString; public int myInt; public Listbox myListbox; } 
+3
source

If you are looking for a list of strings, int, textbox, you can either create a class that has these members, or look at the Tuple class in .net 4.0

List<Tuple<string,int,TextBox>

+1
source

Define a class that contains 3 types, then define an array that contains the new type.

0
source

Object [] myObjects = new Object () {"myString", 42, textbox1};

0
source
 System.Collections.Generic.Dictionary<string, object> source = new System.Collections.Generic.Dictionary<string, object>(); source.Add("A", "Hi"); source.Add("B", 10); source.Add("C", new TextBox()); 

When accessing

 string str = Convert.ToString(source["A"]); int id = Convert.ToInt16(source["B"]); TextBox t = (TextBox)source["C"]; 
0
source

I suggest creating a type like

 enum ItemType { Int, String, Textbox } class MyType { public object objValue; public ItemType itemType; } List<MyType> list = new List<MyType>(); ....... 

You can iterate over the list or retrieve the list by type, for example, below.

 var intList = list.Where(e=>e.itemType == ItemType.Int); 

Of course, you can achieve the above with an enumeration and use the reflected type information directly from the object, but I just think it is more understandable, so I explicitly list the type that your list can contain, and not just the entire type in the CLR

0
source

Source: https://habr.com/ru/post/887729/


All Articles