Part 1 Can you store different types in an array in c#

preview_player
Показать описание
If you are a foodie like me, I am sure you will enjoy the recipes on my friend's YouTube channel. If you find them useful, please subscribe and share to support her. She's really good at what she does.

Link for all dot net and sql server video tutorial playlists

Can you store different types in an array in c#?
Yes, if you create an object array. Here is an example. Since all types inherit (directly or indirectly) from object type, we can add any type to the object array, including complex types like Customer, Employee etc. You need to override the ToString() method if you want meaningful output when ToString() method is invoked.

class Program
{
static void Main()
{
object[] objectArray = new object[3];
objectArray[0] = 101; // integer
objectArray[1] = "C#"; // string

Customer c = new Customer();
c.ID = 99;
c.Name = "Pragim";

objectArray[2] = c; // Customer - Complext Type

// loop thru the array and retrieve the items
foreach (object obj in objectArray)
{
Console.WriteLine(obj);
}
}
}

class Customer
{
public int ID { get; set; }
public string Name { get; set; }

public override string ToString()
{
return this.Name;
}
}

Another alternative is to use ArrayList class that is present in System.Collections namespace.
class Program
{
static void Main()
{
System.Collections.ArrayList arrayList = new System.Collections.ArrayList();
arrayList.Add(101); // integer
arrayList.Add("C#"); // integer

Customer c = new Customer();
c.ID = 99;
c.Name = "Pragim";

arrayList.Add(c); // Customer - Complext Type

// loop thru the array and retrieve the items
foreach (object obj in arrayList)
{
Console.WriteLine(obj);
}
}
}
Рекомендации по теме
Комментарии
Автор

Hi Saagar, sure, please feel free to ask any question that you have faced in an interview.

Csharp-video-tutorialsBlogspot
Автор

Sure Saif, will record and upload very soon. In the description of this video, I have included the link for ASP .NET, C#, and SQL Server playlists. All the videos are arranged in logical sequence in these playlists, which could be useful to you. Please share the link with your friends who you think would also benefit from them. If you like these videos, please click on the THUMBS UP button below the video. For email alerts, when new videos are uploaded, you may subscribe to my channel.

Csharp-video-tutorialsBlogspot
Автор

Hi Mandy, sure, will record a video on this very soon. Thank you for asking.

Csharp-video-tutorialsBlogspot
Автор

You are unreal..nobody can be so good so humble so knowledgeable and so perfect..Thank you sir.

piyasidey
Автор

The best videos on the net so far....thanks a lot for providing these. It is helping me to refresh my knowledge and of course learn new stuff as well.

satyenkasturi
Автор

Hi Chandrashekar, I agree with you. This is going to help a lot of people. Sure, will definitely do it as soon as I can.

Csharp-video-tutorialsBlogspot
Автор

Sir I learn so many things from your tutorials and selected in a company Thank You ..God Bless You teaching skill is awesome..

saurabhchauhan
Автор

Watched a few of these, very informative, thank you!

benjaminfornataro
Автор

I just love your you for sharing venkat you so much sir

hemantverma
Автор

Great question...I don't see any difference. The same example works with List<object as well. Thanks for asking such a good question :-)

Csharp-video-tutorialsBlogspot
Автор

Thank you very much Venkat, you are great presenter . all you are presented is very very clear, Thank you for helping people.

omymma
Автор

Indeed it's going to help a lot of people. It's a very generous thing you are doing here with all these tutorial lesson explained in a very understandable way. Bless you. YOU ROCK :)'

MrKishoreS
Автор

Hi Saurabh, Congratulations. I am very happy for you. Good luck and all the very best. Thank you very much, for sharing the good news. I am honoured.

Csharp-video-tutorialsBlogspot
Автор

Thanks again for another great c# tutorial explaining data types stored in arrays.

dwlg
Автор

We need an updated playlist sir, your doing a great job

umofowish
Автор

OMG
I don't know why I didn't come here before.
Awesome

abhishekvishwakarma
Автор

this series is going to be the best series sir for such a great series

iamsmzubair
Автор

Thank you for the quick answering, thumbed up :)

df_
Автор

wow sir this series is going to be biggest help for us...long live Venkat sir..
thanks sir..

Kumar-idqs
Автор

thnx a sir...i will also be asking questions..so that it will also be helpful for other developers, well ur videos on diff topics are so clearly covered..that there are less chances, u mostly cover all topics....sir ur naming conventions are really very very simple and easy to understand...thnx a lot

saagarsoni
visit shbcf.ru