Click here to Skip to main content
11,411,241 members (65,596 online)
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.3K
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 Sergey Alexandrovich Kryukov 189
1 Maciej Los 108
2 OriginalGriff 88
3 Sascha Lefévre 85
4 BillWoodruff 80
0 Sergey Alexandrovich Kryukov 8,920
1 OriginalGriff 6,918
2 Maciej Los 3,390
3 Abhinav S 3,248
4 Peter Leow 3,059


Advertise | Privacy | Mobile
Web04 | 2.8.150414.5 | Last Updated 1 Aug 2011
Copyright © CodeProject, 1999-2015
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