Unknown number of properties using generics


  • Hi guys,

    I have a question concerning the generics.

    Imagine this scenario:

    I want to create a class object, and add to this object a X number of properties.

    class ClassTest
       private String x1;
       private String x2;
       private String x3;
       public ClassTest()
       public String X1
          get { return x1; }
          set { this.x1 = value; }
       public String X2
          get { return x2; }
          set { this.x2 = value; }
       public String X3
          get { return x3; }
          set { this.x3 = value; }

    This class object has for example 3 properties, that you can set and get their values.

    But how can i make a class object with an unknown number of properties ? Let's say that i have a TextBox and i enter there a random number of properties, and then another TextBox that you enter a value and set it for the first propertie, then for the second and so on until the X number of properties end ?

    And after we set the values for the properties, how can we get them ?

    Thank You!

    Best Regards. 

    Friday, July 23, 2010 8:21 AM


All replies

  • Hi!

    I think you can use a List or Dictionary if you're uncertain about the number of objects you'll have to store.

    Using a List, you can store different values and later acces them by index. In a Dictionary, you can mark every stored value with a key, and access the values later using this key. Here's a simple example:

    class TestClass
      private Dictionary<string, string> strings = new Dictionary<string, string>();
      public void AddNew(string key, string value)
        strings.Add(key, value);
      public string GetValue(string key)
        return strings[key];

    Of course this is just a sample, you'll have to catch exceptions, perhaps make the class generic etc... But using this method (or the simpler with lists) you can store any number of properties (strings in this example).

    The other choice is dynamic expandos - if you're using C#4/.NET4 - but dynamic code is a bit more error prone and can hurt performance.

    Hope I could help!


    Tuesday, July 27, 2010 5:04 PM
  • This post motivated me to write on this subject.

    C#: Multiple Dynamic Properties on a Class Either Defined or with Generics

    I provide examples such as

    myclass["DynamicProperty1"] = "test";

    as well as a generic method such as

    myClass<string, string> mc = new mc<string,string>();

    myclass["DynamicProperty1"] = "test";

    or via enums as well. HTH

    William Wegerson (www.OmegaCoder.Com)
    Tuesday, July 27, 2010 6:03 PM
  • Yes, like already said, use a list<T> or Dictionary<K,V>. I dont really see why you would want to do that for properties because properties should be used for other classes to access that data either/or by retrieving the value or/and setting it. 

    You can't make a property from a collection. Another way you can do it is by iterating through the Controls of the form. Make the textBox.Text to public /internal (depending on namespace)

    List<string> GetTextBoxString()

    { List<string> list = new List<string>()


    foreach (Control c in this.Controls)


          if (c.GetType() == typeof(TextBox))


    return list;



    public string[] TB_TextArray


         get {return this.GetTextBoxString(this.Controls).ToArray();}

         set {             

                   this.GetTextBoxString(this.Controls).ToArray() = value;


    Wednesday, July 28, 2010 3:32 AM