Click here to Skip to main content
15,889,867 members
Please Sign up or sign in to vote.
1.00/5 (4 votes)
See more:
hi, is there a way to store different type of object in something like list,Dictionry ....... ?

What I have tried:

i've tried list"Controle" and List"T" but it dosn't work <>
Posted
Comments
Sergey Alexandrovich Kryukov 12-Feb-16 11:53am    
This is not a correct question. You probably don't understand the principles. Don't mix polymorphism and generics; learn both concepts. You need to understand compile-time types vs runtime types...
—SA
Philippe Mori 12-Feb-16 20:04pm    
System.Collections from .NET 1.x does just that. But you should not use them. One of the purpose of generic is to avoid untyped containers.

There is, you just need to sort the syntax out.
First you need this line in your code file:

C#
using System.Collections.Generic;


Then you can declare your collection:

C#
var foo = new List<int>(); //List of integers
var bar = new Dictionary<string,int>();// A dictionary of bools, using a string as a key
var baz= new Queue<myclass>(); // A queue of objects of type myClass

etc...

The List<T> and List(T) you see on MSDN are really there to allow developers know the type is generic (the T part is the generic that needs to be specified, and is specified in different ways in the various languages supported). This is confusing when you are first getting started....
 
Share this answer
 
v4
Comments
Member 11736914 12-Feb-16 11:45am    
what i need is to have something like this <pre lang="c#"><pre>var Something = new Something ();
Something .add(FirstClass);
Something .add(SecondClass);</pre></pre>
Keith Barrow 12-Feb-16 11:51am    
First, you need to know the type of FirstClass and SecondClass - they must be of the same type of have a common super-type in their inheritance chain. Both will be of type object, but you should avoid that if possible.
Second you need to figure out the best collection type for what you are doing- generally List<int> works in *roughly* the same way as int[]. List<t> isn't necessarily the correct solution.
Richard Deeming 12-Feb-16 12:41pm    
The second type parameter for your dictionary seems to have been lost.
Keith Barrow 13-Feb-16 6:27am    
Fixed, thanks!
i found a solution i solved it using a class that inharits from DinamicObject and the i decared my variable
C#
private Dictionary<string,> _Props = new Dictionary<string,>();
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900