Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# collections Generics
Hi experts,
 
we can create a list of known bytes in two similar ways:
List<byte> withParentheses = new List<byte>() {1,2,3,4,5};
List<byte> withoutParentheses = new List<byte> {6,7,8,9,10};
Both seem to work.
 
What is the difference between the two?
Is one of them preferrable over the other?
Posted 1-Aug-11 2:41am
lukeer15.1K
Edited 1-Aug-11 2:47am
v2
Comments
BobJanova at 1-Aug-11 11:05am
   
Heh, I didn't know this was possible. Thanks for the tip!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Nothing, as far as your example is concerned.
 
In the first case, you could actually define the array count in the first statement but add values to it later.
E.g.
List withParentheses = new List[4];
withParentheses[0]=1;
withParentheses[1]=2;
 
In the second case, you don't have that liberty. You must define some initial values in the array (as you have done).
  Permalink  
v4
Comments
   
5 - proposed as answer
Abhinav S at 1-Aug-11 8:06am
   
Thank you.
SAKryukov at 1-Aug-11 10:29am
   
Agree, my 5.
A short comment "you don't have that liberty". In exchange of liberty of not supplying elements at the point of initialization you get another liberty of not specifying the length -- in either form.
--SA
Abhinav S at 1-Aug-11 10:32am
   
Agreed.
Thank you SA.
lukeer at 2-Aug-11 3:05am
   
My question did not contain sqare brackets. Do I understand you correctly, that sqare brackets for arrays behave exactly like parentheses for generic lists (within the given context)?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

It might be helpful,
 
CSharp 3 0overview[^]
 
Smile | :)
  Permalink  
Comments
SAKryukov at 1-Aug-11 10:31am
   
The good idea is: read C# manual (or reference before asking), but: why 3.0? what part of it answers the question?
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 George Jonsson 215
1 Kornfeld Eliyahu Peter 169
2 OriginalGriff 120
3 PIEBALDconsult 110
4 BillWoodruff 85
0 OriginalGriff 6,165
1 DamithSL 4,658
2 Maciej Los 4,087
3 Kornfeld Eliyahu Peter 3,649
4 Sergey Alexandrovich Kryukov 3,294


Advertise | Privacy | Mobile
Web01 | 2.8.141220.1 | Last Updated 1 Aug 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100