Faisal Pathan
Can we store mixed datatypes such as char, string ,int etc. in single array?
By Faisal Pathan in C# on Jun 06 2018
  • Samita Sahu
    Jul, 2018 6

    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");

    • 3
  • Melbin m p
    Oct, 2018 7

    Yes you can use ArrayList Collection which is Type Independent

    • 1
  • Ayappan Alagesan
    Jul, 2018 31

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

    • 1
  • Chandan Prasad
    Jul, 2018 27

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

    • 1
  • Udit Chauhan
    Jul, 2018 18

    using ArrayList we can do that

    • 1
  • Kalyani Shevale
    Jun, 2018 26

    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];

    • 1
  • Faisal Pathan
    Jun, 2018 6

    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; }

    • 1
  • Rajeev Kumar
    Mar, 2023 20

    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:

    • 0
  • Hovhannes Movsisyan
    Feb, 2020 28

    Yes

    • 0
  • Hovhannes Movsisyan
    Feb, 2020 28

    Yes

    • 0
  • Bidyasagar Mishra
    Aug, 2019 4

    array it wont possible

    • 0
  • Kaushik Dudhat
    Feb, 2019 13

    yes

    • 0
  • Shivam Shukla
    Jun, 2018 27

    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.

    • 0


Most Popular Job Functions


MOST LIKED QUESTIONS