Click here to Skip to main content
11,934,380 members (40,386 online)
Rate this:
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
Edited 1-Aug-11 2:47am
BobJanova 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 1-Aug-11 8:06am
Thank you.
SAKryukov 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 1-Aug-11 10:32am
Thank you SA.
lukeer 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 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
Top Experts
Last 24hrsThis month

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