13
Reply

Can we store mixed datatypes such as char, string ,int etc. in single array?

Faisal Pathan

Faisal Pathan

6y
32.1k
5
Reply

    Yes we can store different/mixed types in a single array by using following two methods: Method 1: using Object array because all types in .net inherit from object type Ex: object[] array=new object[2];array[0]=102;array[1]="csharp";Method 2: Alternatively we can use ArrayList class present in System.Collections namespace .We can use Add() method of ArrayList class to add different data types to the ArrayList collection. Ex: ArrayList arrlist=new ArrayList();arrlist.Add(98);arrlist.Add("Dotnet");

    Yes you can use ArrayList Collection which is Type Independent

    We can do that but , ArrayList is the correct way to do that.

    In single array we can't store mixed datatype however by using ArrayList you can achieve this

    using ArrayList we can do that

    Yes, We can use mixed datatypes in a single array.struct {enum { is_int, is_float, is_char } type;union {int ival;float fval;char cval;} val; } my_array[10];

    Yes, We can do.class Example {public string Name {get; set;}public int Age {get; set;} }//Method {object[] MyArray = new object[3];MyArray[0] = "Faisal";MyArray[1] = 100; Example exp = new Example();exp.Name = "Faisal Pathan";exp.Age = 22;MyArray[2] = exp; }

    Yes. we can store different/mixed types in a single array by using following two methods: Method 1: using Object array because all types in .net inherit from object type Ex: object[] array=new object[2];array[0]=102;array[1]=”csharp”;Method 2:

    Yes

    Yes

    array it wont possible

    yes

    You can do the same by many ways one of them is using a union:union {int ival;float fval;void *pval; } array[10]; You will have to keep track of the type of each element, though.You can try it via Class also.