Click here to Skip to main content
15,889,720 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi experts,

we can create a list of known bytes in two similar ways:
C#
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
Updated 1-Aug-11 1:47am
v2
Comments
BobJanova 1-Aug-11 11:05am    
Heh, I didn't know this was possible. Thanks for the tip!

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<byte> withParentheses = new List<byte>[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).
 
Share this answer
 
v4
Comments
#realJSOP 1-Aug-11 8:03am    
5 - proposed as answer
Abhinav S 1-Aug-11 8:06am    
Thank you.
Sergey Alexandrovich Kryukov 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 1-Aug-11 10:32am    
Agreed.
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)?
It might be helpful,

CSharp 3 0overview[^]

:)
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900