Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Possible to have a List of Generic items of different types?

Possible to have a List of Generic items of different types?

Scheduled Pinned Locked Moved C#
question
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Paul Brower
    wrote on last edited by
    #1

    Forgive the 'generic' formatting, I don't seem to be able to post 'proper' generic code here. I have a simple class (see below). I need to have a way to have a SINGLE collection of 'GenericDataElement' objects. Is this possible? Some of the items may be GenericDataElement<>, while others may be GenericDataElement<>. public class GenericDataElement<> { private string name; private T value; public string Name { get { return name; } } public T Value { get { return value; } } public GenericDataElement(string name, T value) { this.name = name; this.value = value; } }

    U 1 Reply Last reply
    0
    • P Paul Brower

      Forgive the 'generic' formatting, I don't seem to be able to post 'proper' generic code here. I have a simple class (see below). I need to have a way to have a SINGLE collection of 'GenericDataElement' objects. Is this possible? Some of the items may be GenericDataElement<>, while others may be GenericDataElement<>. public class GenericDataElement<> { private string name; private T value; public string Name { get { return name; } } public T Value { get { return value; } } public GenericDataElement(string name, T value) { this.name = name; this.value = value; } }

      U Offline
      U Offline
      Urs Enzler
      wrote on last edited by
      #2

      It isn't possible to have a strongly typed generic list that allows 2 different type parameters, but you can do something like that: public class GenericDataElement { } public class GenericDataElement : GenericDataElement { public GenericDataElement(string name, T value) { this.name = name; this.value = value; } private string name; public string Name { get { return name; } } private T value; public T Value { get { return value; } } } public class Test { List list = new List(); public void TestIt() { list.Add(new GenericDataElement("string", "value")); list.Add(new GenericDataElement("int", 1)); foreach (GenericDataElement gde in list) { if (gde is GenericDataElement) Console.WriteLine("int"); else Console.WriteLine("string"); } } } But you will have an untyped collection, of course. -^-^-^-^-^- no risk no funk ................... please vote ------>

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups