By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,994 Members | 2,073 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,994 IT Pros & Developers. It's quick & easy.

Creation of List<T>

P: 13
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?
Jan 24 '09 #1
Share this Question
Share on Google+
1 Reply


Expert 100+
P: 190
The easiest way is
Expand|Select|Wrap|Line Numbers
  1. 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.
Expand|Select|Wrap|Line Numbers
  1.   class SetClass {
  2.     uint[] set = new uint[4];
  3.     public SetClass() { }
  4.     public uint[] Set {
  5.       get {
  6.         if (this.set == null)
  7.           this.set = new uint[4];
  8.         return this.set; 
  9.       }
  10.       set {
  11.         if (value.Length != 4)
  12.           throw new ArgumentException("Length must be four.");
  13.         this.set = value;
  14.       }
  15.     }
  16.   }
and then you could do:
Expand|Select|Wrap|Line Numbers
  1. List<SetClass> SetCollection = new List<SetClass>();
or even create a struct with four uint properties instead of an array to really enforce the length.
Jan 25 '09 #2

Post your reply

Sign in to post your reply or Sign up for a free account.