Creation of List<T>

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • SoftwareTester
    New Member
    • Jan 2008
    • 13

    #1

    Creation of List<T>

    i know how to create a simple list like
    List<UInt32> Sets = new List<UInt32>();
    and i can add simple UInt32 objects to it

    but now I want to create a such a List<..> of small arrays consisting of 4 elements each of UIn32 so that i can add
    UInt32[] Sets = new UInt32[4]; objects to it

    how can i do what i want?
  • mldisibio
    Recognized Expert New Member
    • Sep 2008
    • 191

    #2
    The easiest way is
    Code:
    List<uint[]> ListOfSets = new List<uint[]>;
    but that will not enforce the length of the array. To do so you would have to create a struct or class with some bounds checking.
    Code:
      class SetClass {
        uint[] set = new uint[4];
        public SetClass() { }
        public uint[] Set {
          get {
            if (this.set == null)
              this.set = new uint[4];
            return this.set; 
          }
          set {
            if (value.Length != 4)
              throw new ArgumentException("Length must be four.");
            this.set = value;
          }
        }
      }
    and then you could do:
    Code:
    List<SetClass> SetCollection = new List<SetClass>();
    or even create a struct with four uint properties instead of an array to really enforce the length.

    Comment

    Working...