Monday, February 26, 2007

C# : Predefining an array

The classic way of creating an array is:


Document[] doc = new Document[2];
doc[0] = new Document();
doc[0].code = "ABC";
doc[1] = new Document();
doc[1].code = "DEF";


but a more elegant way is:


Document doc1 = new Document();
doc1.code = "ABC";
Document doc2 = new Document();
doc2.code = "DEF";
? = new Document[] {doc1, doc2};


Enjoy!

No comments: