Link to home
Start Free TrialLog in
Avatar of Mr_Fulano
Mr_FulanoFlag for United States of America

asked on

A Collection that allows for multiple types in C#

Hello, I've done some research online, but haven't found a clear answer to my questions, so I am seeking advice. I believe it can be done in Apple's Swift programming language, when using arrays, but I haven't seen a way to do it in Visual Studio C#.


What I would like to do is have a List<> of BOTH ints and chars. Such as:

 {1, 2, 3, 4, A, B, C, D}.     --- Or any Generic Collection Type in C#.


*** There will always be 4 ints and 4 chars. There will also always be 8 total elements to the List or Collections. 


I did find an example that used a KeyValuePair, but that is only one Key and one Value, which is essentially a Dictionary, and which won't work for my needs. 


I'm not sure that what I'm trying to do can be done without creating a custom class that contains 8 elements, four of which are ints and four of which are chars.


Am I wrong, or is there a way to create a List<ANY-TYPE>... or maybe an Array[ANY-TYPE]?

 

Is there a collection that allows for this type of functionality other than the List class? 


Thank you for your help.


ASKER CERTIFIED SOLUTION
Avatar of ste5an
ste5an
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Mr_Fulano

ASKER

Hello Ste5an, WOW.... very interesting.  I was thinking that "Type" would only mean <int> or <string> or <char>, but <object> makes perfect sense. I did think of object, but ruled it out, thinking that no, object wouldn't be a "Type" per say, but that was completely wrong on my part.

Thank you very much for clarifying that for me. Much appreciated...!!!

 
Hello Ste5an, just as a quick follow-up to my question.  So, your response definitely works, is absolutely correct, and is exactly what I needed, but it bring up the question of - how does that affect memory usage?

In other words, it seems that "object" is the perfect choice, and would be for any case, but why don't we just use "object" for everything rather than specifying - int, double, long, string, char, etc...?

Given that specifying the Type allows the compiler to allocate memory effectively?  How does the compiler handle an "object"... does it just say, OK, I have an object, which I know is  an Integer, so I'll allocate enough memory storage for an Integer, and I also have an object that is a Char, so I'll allocate enough memory storage for a Char... and so on, or does it use large  blocks of memory storage and allow for wasted memory?

If you could share some insight into that, it would help me learn.

Thank you!
Using object for everything means leaving the path of strong and early typing and going towards duck typing.
I do not know what "duck typing" means... please explain your comments in better detail.
It means that type safety is ensured by interface identity. When two instances have the same public members, then they are considered the same. Thus, they are assignment-compatible.

"When it walks like a duck, and quacks like a duck, and looks like a duck, then it must be a duck."