0 votes
in C Sharp by
Is it possible to store different datatypes in one array like Int String Float and Char?

1 Answer

0 votes
by
Yes, it is possible to store different datatypes or we can say mixed datatypes in one array. But the array type should be of type object that can store any datatype but also the object of the class as given in below example

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace consoleAppExample

{

 class MainProgram

 {

  class SubProgram

  {

   public int EmpId {get; set;}

   public string EmpName { get; set; }

   public override string ToString()

   { return this.Name; }

 }

 static void Main(string[] args)

 {

  //sample of array of object type

  object[] array = new object[3];

  array[0]=1234;

  array[1]="madanswer.com";

  SubProgram subobj=new SubProgram();

  subobj.EmpId =567;

  subobj.EmpName="Adam";

  //Assign object of class to array as 3rd array

  array[2]=subobj;

  }

 }

}

Related questions

0 votes
asked Jun 16, 2020 in C Sharp by Hodge
0 votes
asked Jun 16, 2020 in C Sharp by Hodge
...