Click here to Skip to main content
11,799,282 members (78,983 online)
Rate this: bad
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 1:41am
Edited 1-Aug-11 1:47am
BobJanova at 1-Aug-11 11:05am
Heh, I didn't know this was possible. Thanks for the tip!
Rate this: bad
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.
List withParentheses = new List[4];

In the second case, you don't have that liberty. You must define some initial values in the array (as you have done).
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.
Abhinav S at 1-Aug-11 10:32am
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
Please Sign up or sign in to vote.

Solution 2

It might be helpful,

CSharp 3 0overview[^]

Smile | :)
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?

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

  Print Answers RSS
0 OriginalGriff 473
1 CPallini 410
2 Richard MacCutchan 319
3 phil.o 244
4 Kornfeld Eliyahu Peter 205
0 OriginalGriff 2,475
1 Maciej Los 1,860
2 KrunalRohit 1,496
3 CPallini 1,465
4 Richard MacCutchan 1,149

Advertise | Privacy | Mobile
Web01 | 2.8.151002.1 | 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