Why declare twice the type of a list in c#? [closed]
up vote
-2
down vote
favorite
So, i'm just wondering why do we need to declare the type of a list two times on c#, as the example below:
List<Product> products = new List<Product>();
i worked with Lists (which are ArrayLists) on Java, but there we don't have to do this necessarily.
c# .net
New contributor
closed as off-topic by Daniel A. White, D-Shih, OmG, Camilo Terevinto, Manfred Radlwimmer Nov 10 at 16:11
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "This question was caused by a problem that can no longer be reproduced or a simple typographical error. While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers. This can often be avoided by identifying and closely inspecting the shortest program necessary to reproduce the problem before posting." – D-Shih, Camilo Terevinto
|
show 6 more comments
up vote
-2
down vote
favorite
So, i'm just wondering why do we need to declare the type of a list two times on c#, as the example below:
List<Product> products = new List<Product>();
i worked with Lists (which are ArrayLists) on Java, but there we don't have to do this necessarily.
c# .net
New contributor
closed as off-topic by Daniel A. White, D-Shih, OmG, Camilo Terevinto, Manfred Radlwimmer Nov 10 at 16:11
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "This question was caused by a problem that can no longer be reproduced or a simple typographical error. While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers. This can often be avoided by identifying and closely inspecting the shortest program necessary to reproduce the problem before posting." – D-Shih, Camilo Terevinto
2
var products = new List<Product>();
– Klaus Gütter
Nov 10 at 14:20
2
It is declaring the type just once - the RHS is creating an instance
– Disaffected 1070452
Nov 10 at 14:23
5
Simply Java != C#.
– Daniel A. White
Nov 10 at 14:24
5
It is a limitation of the C# type inference feature. Decently explained in this blog post.
– Hans Passant
Nov 10 at 14:28
1
@Hans Passant Good point - it was not clear from the question that it is a field, not a variable.
– Klaus Gütter
Nov 10 at 14:33
|
show 6 more comments
up vote
-2
down vote
favorite
up vote
-2
down vote
favorite
So, i'm just wondering why do we need to declare the type of a list two times on c#, as the example below:
List<Product> products = new List<Product>();
i worked with Lists (which are ArrayLists) on Java, but there we don't have to do this necessarily.
c# .net
New contributor
So, i'm just wondering why do we need to declare the type of a list two times on c#, as the example below:
List<Product> products = new List<Product>();
i worked with Lists (which are ArrayLists) on Java, but there we don't have to do this necessarily.
c# .net
c# .net
New contributor
New contributor
edited Nov 10 at 14:24
Daniel A. White
147k35290371
147k35290371
New contributor
asked Nov 10 at 14:19
Marcus Ruas
14
14
New contributor
New contributor
closed as off-topic by Daniel A. White, D-Shih, OmG, Camilo Terevinto, Manfred Radlwimmer Nov 10 at 16:11
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "This question was caused by a problem that can no longer be reproduced or a simple typographical error. While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers. This can often be avoided by identifying and closely inspecting the shortest program necessary to reproduce the problem before posting." – D-Shih, Camilo Terevinto
closed as off-topic by Daniel A. White, D-Shih, OmG, Camilo Terevinto, Manfred Radlwimmer Nov 10 at 16:11
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "This question was caused by a problem that can no longer be reproduced or a simple typographical error. While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers. This can often be avoided by identifying and closely inspecting the shortest program necessary to reproduce the problem before posting." – D-Shih, Camilo Terevinto
2
var products = new List<Product>();
– Klaus Gütter
Nov 10 at 14:20
2
It is declaring the type just once - the RHS is creating an instance
– Disaffected 1070452
Nov 10 at 14:23
5
Simply Java != C#.
– Daniel A. White
Nov 10 at 14:24
5
It is a limitation of the C# type inference feature. Decently explained in this blog post.
– Hans Passant
Nov 10 at 14:28
1
@Hans Passant Good point - it was not clear from the question that it is a field, not a variable.
– Klaus Gütter
Nov 10 at 14:33
|
show 6 more comments
2
var products = new List<Product>();
– Klaus Gütter
Nov 10 at 14:20
2
It is declaring the type just once - the RHS is creating an instance
– Disaffected 1070452
Nov 10 at 14:23
5
Simply Java != C#.
– Daniel A. White
Nov 10 at 14:24
5
It is a limitation of the C# type inference feature. Decently explained in this blog post.
– Hans Passant
Nov 10 at 14:28
1
@Hans Passant Good point - it was not clear from the question that it is a field, not a variable.
– Klaus Gütter
Nov 10 at 14:33
2
2
var products = new List<Product>();
– Klaus Gütter
Nov 10 at 14:20
var products = new List<Product>();
– Klaus Gütter
Nov 10 at 14:20
2
2
It is declaring the type just once - the RHS is creating an instance
– Disaffected 1070452
Nov 10 at 14:23
It is declaring the type just once - the RHS is creating an instance
– Disaffected 1070452
Nov 10 at 14:23
5
5
Simply Java != C#.
– Daniel A. White
Nov 10 at 14:24
Simply Java != C#.
– Daniel A. White
Nov 10 at 14:24
5
5
It is a limitation of the C# type inference feature. Decently explained in this blog post.
– Hans Passant
Nov 10 at 14:28
It is a limitation of the C# type inference feature. Decently explained in this blog post.
– Hans Passant
Nov 10 at 14:28
1
1
@Hans Passant Good point - it was not clear from the question that it is a field, not a variable.
– Klaus Gütter
Nov 10 at 14:33
@Hans Passant Good point - it was not clear from the question that it is a field, not a variable.
– Klaus Gütter
Nov 10 at 14:33
|
show 6 more comments
2 Answers
2
active
oldest
votes
up vote
6
down vote
You don't. It is perfectly fine to just write
var products = new List<Product>();
as long as the variable is declared within the scope of a method.
See https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/var
2
It is also recomended if right part visibly define its type.
– eocron
Nov 10 at 14:36
@eocron Yes, exactly. I always try to remember to use var unless the type cannot visibly be inferred from the right part. But one place I keep forgetting to use it out of old habit is within for-loops, e.g.for (var i = 0; i < 10; i++) ...
– runcoderun
Nov 10 at 14:57
Speaking about fields this answer is misleading to say the least.
– Antonín Lejsek
Nov 10 at 15:45
@Antonín Lejsek I just added clarification to avoid any misunderstandings (although it is not clear to me that the question is about fields + the article I linked to actually states right at the top that the variable must be declared at method scope).
– runcoderun
Nov 10 at 16:29
"why do we need to declare the type of a list two times" - this implies that we are talking about situations where we do have to. In other words, this sentence rules out cases where we do not have to declare the type twice (like methods variables).
– Antonín Lejsek
Nov 10 at 16:38
add a comment |
up vote
-3
down vote
You are not delcaring the same thing twice.
List<Product> products;
declares the variable of that type.
whereas
products = new List<Product>();
creates a instance and assigns it to the variable.
List<Product> products = new List<Product>();
is simply a way to do both operations in one line.
Also note this regarding cases where the variable type and newly created isntance do not match: https://stackoverflow.com/a/12321186/3346583 Apparently that is somewhat more common in Java, as I actually found it when looking for java examples.
NeitherList<object> products = new List<Product>();
norList<Product> products = new List<object>();
are valid, or am i missing something? Nice reference to the comic by the way (reminds me of that legendary "WTFJS" video about Javascript ;-) )
– elgonzo
Nov 10 at 14:46
@elgonzo: I am not 100% sure it is right, but you are propably missing covaraince and contravariance: blogs.msdn.microsoft.com/csharpfaq/2010/02/16/… Took me some time to learn of it myself. And I still have not fully wrapped my head around it.
– Christopher
Nov 10 at 14:48
I think you misunderstood something there. Your code is trying to tell me i can stuff any non-product object into a product list (List<Product>) because it is a list of objects... ahem... (List<T> is not IEnumerable<T>!!!)
– elgonzo
Nov 10 at 14:48
@elgonzo: Co- and Contravariance apply to every generic type. But you are right that my second example would not work. I need to fix that.
– Christopher
Nov 10 at 14:52
1
@elgonzo: removed that part.
– Christopher
Nov 10 at 15:18
|
show 4 more comments
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
6
down vote
You don't. It is perfectly fine to just write
var products = new List<Product>();
as long as the variable is declared within the scope of a method.
See https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/var
2
It is also recomended if right part visibly define its type.
– eocron
Nov 10 at 14:36
@eocron Yes, exactly. I always try to remember to use var unless the type cannot visibly be inferred from the right part. But one place I keep forgetting to use it out of old habit is within for-loops, e.g.for (var i = 0; i < 10; i++) ...
– runcoderun
Nov 10 at 14:57
Speaking about fields this answer is misleading to say the least.
– Antonín Lejsek
Nov 10 at 15:45
@Antonín Lejsek I just added clarification to avoid any misunderstandings (although it is not clear to me that the question is about fields + the article I linked to actually states right at the top that the variable must be declared at method scope).
– runcoderun
Nov 10 at 16:29
"why do we need to declare the type of a list two times" - this implies that we are talking about situations where we do have to. In other words, this sentence rules out cases where we do not have to declare the type twice (like methods variables).
– Antonín Lejsek
Nov 10 at 16:38
add a comment |
up vote
6
down vote
You don't. It is perfectly fine to just write
var products = new List<Product>();
as long as the variable is declared within the scope of a method.
See https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/var
2
It is also recomended if right part visibly define its type.
– eocron
Nov 10 at 14:36
@eocron Yes, exactly. I always try to remember to use var unless the type cannot visibly be inferred from the right part. But one place I keep forgetting to use it out of old habit is within for-loops, e.g.for (var i = 0; i < 10; i++) ...
– runcoderun
Nov 10 at 14:57
Speaking about fields this answer is misleading to say the least.
– Antonín Lejsek
Nov 10 at 15:45
@Antonín Lejsek I just added clarification to avoid any misunderstandings (although it is not clear to me that the question is about fields + the article I linked to actually states right at the top that the variable must be declared at method scope).
– runcoderun
Nov 10 at 16:29
"why do we need to declare the type of a list two times" - this implies that we are talking about situations where we do have to. In other words, this sentence rules out cases where we do not have to declare the type twice (like methods variables).
– Antonín Lejsek
Nov 10 at 16:38
add a comment |
up vote
6
down vote
up vote
6
down vote
You don't. It is perfectly fine to just write
var products = new List<Product>();
as long as the variable is declared within the scope of a method.
See https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/var
You don't. It is perfectly fine to just write
var products = new List<Product>();
as long as the variable is declared within the scope of a method.
See https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/var
edited Nov 10 at 16:11
answered Nov 10 at 14:20
runcoderun
1816
1816
2
It is also recomended if right part visibly define its type.
– eocron
Nov 10 at 14:36
@eocron Yes, exactly. I always try to remember to use var unless the type cannot visibly be inferred from the right part. But one place I keep forgetting to use it out of old habit is within for-loops, e.g.for (var i = 0; i < 10; i++) ...
– runcoderun
Nov 10 at 14:57
Speaking about fields this answer is misleading to say the least.
– Antonín Lejsek
Nov 10 at 15:45
@Antonín Lejsek I just added clarification to avoid any misunderstandings (although it is not clear to me that the question is about fields + the article I linked to actually states right at the top that the variable must be declared at method scope).
– runcoderun
Nov 10 at 16:29
"why do we need to declare the type of a list two times" - this implies that we are talking about situations where we do have to. In other words, this sentence rules out cases where we do not have to declare the type twice (like methods variables).
– Antonín Lejsek
Nov 10 at 16:38
add a comment |
2
It is also recomended if right part visibly define its type.
– eocron
Nov 10 at 14:36
@eocron Yes, exactly. I always try to remember to use var unless the type cannot visibly be inferred from the right part. But one place I keep forgetting to use it out of old habit is within for-loops, e.g.for (var i = 0; i < 10; i++) ...
– runcoderun
Nov 10 at 14:57
Speaking about fields this answer is misleading to say the least.
– Antonín Lejsek
Nov 10 at 15:45
@Antonín Lejsek I just added clarification to avoid any misunderstandings (although it is not clear to me that the question is about fields + the article I linked to actually states right at the top that the variable must be declared at method scope).
– runcoderun
Nov 10 at 16:29
"why do we need to declare the type of a list two times" - this implies that we are talking about situations where we do have to. In other words, this sentence rules out cases where we do not have to declare the type twice (like methods variables).
– Antonín Lejsek
Nov 10 at 16:38
2
2
It is also recomended if right part visibly define its type.
– eocron
Nov 10 at 14:36
It is also recomended if right part visibly define its type.
– eocron
Nov 10 at 14:36
@eocron Yes, exactly. I always try to remember to use var unless the type cannot visibly be inferred from the right part. But one place I keep forgetting to use it out of old habit is within for-loops, e.g.
for (var i = 0; i < 10; i++) ...
– runcoderun
Nov 10 at 14:57
@eocron Yes, exactly. I always try to remember to use var unless the type cannot visibly be inferred from the right part. But one place I keep forgetting to use it out of old habit is within for-loops, e.g.
for (var i = 0; i < 10; i++) ...
– runcoderun
Nov 10 at 14:57
Speaking about fields this answer is misleading to say the least.
– Antonín Lejsek
Nov 10 at 15:45
Speaking about fields this answer is misleading to say the least.
– Antonín Lejsek
Nov 10 at 15:45
@Antonín Lejsek I just added clarification to avoid any misunderstandings (although it is not clear to me that the question is about fields + the article I linked to actually states right at the top that the variable must be declared at method scope).
– runcoderun
Nov 10 at 16:29
@Antonín Lejsek I just added clarification to avoid any misunderstandings (although it is not clear to me that the question is about fields + the article I linked to actually states right at the top that the variable must be declared at method scope).
– runcoderun
Nov 10 at 16:29
"why do we need to declare the type of a list two times" - this implies that we are talking about situations where we do have to. In other words, this sentence rules out cases where we do not have to declare the type twice (like methods variables).
– Antonín Lejsek
Nov 10 at 16:38
"why do we need to declare the type of a list two times" - this implies that we are talking about situations where we do have to. In other words, this sentence rules out cases where we do not have to declare the type twice (like methods variables).
– Antonín Lejsek
Nov 10 at 16:38
add a comment |
up vote
-3
down vote
You are not delcaring the same thing twice.
List<Product> products;
declares the variable of that type.
whereas
products = new List<Product>();
creates a instance and assigns it to the variable.
List<Product> products = new List<Product>();
is simply a way to do both operations in one line.
Also note this regarding cases where the variable type and newly created isntance do not match: https://stackoverflow.com/a/12321186/3346583 Apparently that is somewhat more common in Java, as I actually found it when looking for java examples.
NeitherList<object> products = new List<Product>();
norList<Product> products = new List<object>();
are valid, or am i missing something? Nice reference to the comic by the way (reminds me of that legendary "WTFJS" video about Javascript ;-) )
– elgonzo
Nov 10 at 14:46
@elgonzo: I am not 100% sure it is right, but you are propably missing covaraince and contravariance: blogs.msdn.microsoft.com/csharpfaq/2010/02/16/… Took me some time to learn of it myself. And I still have not fully wrapped my head around it.
– Christopher
Nov 10 at 14:48
I think you misunderstood something there. Your code is trying to tell me i can stuff any non-product object into a product list (List<Product>) because it is a list of objects... ahem... (List<T> is not IEnumerable<T>!!!)
– elgonzo
Nov 10 at 14:48
@elgonzo: Co- and Contravariance apply to every generic type. But you are right that my second example would not work. I need to fix that.
– Christopher
Nov 10 at 14:52
1
@elgonzo: removed that part.
– Christopher
Nov 10 at 15:18
|
show 4 more comments
up vote
-3
down vote
You are not delcaring the same thing twice.
List<Product> products;
declares the variable of that type.
whereas
products = new List<Product>();
creates a instance and assigns it to the variable.
List<Product> products = new List<Product>();
is simply a way to do both operations in one line.
Also note this regarding cases where the variable type and newly created isntance do not match: https://stackoverflow.com/a/12321186/3346583 Apparently that is somewhat more common in Java, as I actually found it when looking for java examples.
NeitherList<object> products = new List<Product>();
norList<Product> products = new List<object>();
are valid, or am i missing something? Nice reference to the comic by the way (reminds me of that legendary "WTFJS" video about Javascript ;-) )
– elgonzo
Nov 10 at 14:46
@elgonzo: I am not 100% sure it is right, but you are propably missing covaraince and contravariance: blogs.msdn.microsoft.com/csharpfaq/2010/02/16/… Took me some time to learn of it myself. And I still have not fully wrapped my head around it.
– Christopher
Nov 10 at 14:48
I think you misunderstood something there. Your code is trying to tell me i can stuff any non-product object into a product list (List<Product>) because it is a list of objects... ahem... (List<T> is not IEnumerable<T>!!!)
– elgonzo
Nov 10 at 14:48
@elgonzo: Co- and Contravariance apply to every generic type. But you are right that my second example would not work. I need to fix that.
– Christopher
Nov 10 at 14:52
1
@elgonzo: removed that part.
– Christopher
Nov 10 at 15:18
|
show 4 more comments
up vote
-3
down vote
up vote
-3
down vote
You are not delcaring the same thing twice.
List<Product> products;
declares the variable of that type.
whereas
products = new List<Product>();
creates a instance and assigns it to the variable.
List<Product> products = new List<Product>();
is simply a way to do both operations in one line.
Also note this regarding cases where the variable type and newly created isntance do not match: https://stackoverflow.com/a/12321186/3346583 Apparently that is somewhat more common in Java, as I actually found it when looking for java examples.
You are not delcaring the same thing twice.
List<Product> products;
declares the variable of that type.
whereas
products = new List<Product>();
creates a instance and assigns it to the variable.
List<Product> products = new List<Product>();
is simply a way to do both operations in one line.
Also note this regarding cases where the variable type and newly created isntance do not match: https://stackoverflow.com/a/12321186/3346583 Apparently that is somewhat more common in Java, as I actually found it when looking for java examples.
edited Nov 10 at 15:18
answered Nov 10 at 14:38
Christopher
2,3801621
2,3801621
NeitherList<object> products = new List<Product>();
norList<Product> products = new List<object>();
are valid, or am i missing something? Nice reference to the comic by the way (reminds me of that legendary "WTFJS" video about Javascript ;-) )
– elgonzo
Nov 10 at 14:46
@elgonzo: I am not 100% sure it is right, but you are propably missing covaraince and contravariance: blogs.msdn.microsoft.com/csharpfaq/2010/02/16/… Took me some time to learn of it myself. And I still have not fully wrapped my head around it.
– Christopher
Nov 10 at 14:48
I think you misunderstood something there. Your code is trying to tell me i can stuff any non-product object into a product list (List<Product>) because it is a list of objects... ahem... (List<T> is not IEnumerable<T>!!!)
– elgonzo
Nov 10 at 14:48
@elgonzo: Co- and Contravariance apply to every generic type. But you are right that my second example would not work. I need to fix that.
– Christopher
Nov 10 at 14:52
1
@elgonzo: removed that part.
– Christopher
Nov 10 at 15:18
|
show 4 more comments
NeitherList<object> products = new List<Product>();
norList<Product> products = new List<object>();
are valid, or am i missing something? Nice reference to the comic by the way (reminds me of that legendary "WTFJS" video about Javascript ;-) )
– elgonzo
Nov 10 at 14:46
@elgonzo: I am not 100% sure it is right, but you are propably missing covaraince and contravariance: blogs.msdn.microsoft.com/csharpfaq/2010/02/16/… Took me some time to learn of it myself. And I still have not fully wrapped my head around it.
– Christopher
Nov 10 at 14:48
I think you misunderstood something there. Your code is trying to tell me i can stuff any non-product object into a product list (List<Product>) because it is a list of objects... ahem... (List<T> is not IEnumerable<T>!!!)
– elgonzo
Nov 10 at 14:48
@elgonzo: Co- and Contravariance apply to every generic type. But you are right that my second example would not work. I need to fix that.
– Christopher
Nov 10 at 14:52
1
@elgonzo: removed that part.
– Christopher
Nov 10 at 15:18
Neither
List<object> products = new List<Product>();
nor List<Product> products = new List<object>();
are valid, or am i missing something? Nice reference to the comic by the way (reminds me of that legendary "WTFJS" video about Javascript ;-) )– elgonzo
Nov 10 at 14:46
Neither
List<object> products = new List<Product>();
nor List<Product> products = new List<object>();
are valid, or am i missing something? Nice reference to the comic by the way (reminds me of that legendary "WTFJS" video about Javascript ;-) )– elgonzo
Nov 10 at 14:46
@elgonzo: I am not 100% sure it is right, but you are propably missing covaraince and contravariance: blogs.msdn.microsoft.com/csharpfaq/2010/02/16/… Took me some time to learn of it myself. And I still have not fully wrapped my head around it.
– Christopher
Nov 10 at 14:48
@elgonzo: I am not 100% sure it is right, but you are propably missing covaraince and contravariance: blogs.msdn.microsoft.com/csharpfaq/2010/02/16/… Took me some time to learn of it myself. And I still have not fully wrapped my head around it.
– Christopher
Nov 10 at 14:48
I think you misunderstood something there. Your code is trying to tell me i can stuff any non-product object into a product list (List<Product>) because it is a list of objects... ahem... (List<T> is not IEnumerable<T>!!!)
– elgonzo
Nov 10 at 14:48
I think you misunderstood something there. Your code is trying to tell me i can stuff any non-product object into a product list (List<Product>) because it is a list of objects... ahem... (List<T> is not IEnumerable<T>!!!)
– elgonzo
Nov 10 at 14:48
@elgonzo: Co- and Contravariance apply to every generic type. But you are right that my second example would not work. I need to fix that.
– Christopher
Nov 10 at 14:52
@elgonzo: Co- and Contravariance apply to every generic type. But you are right that my second example would not work. I need to fix that.
– Christopher
Nov 10 at 14:52
1
1
@elgonzo: removed that part.
– Christopher
Nov 10 at 15:18
@elgonzo: removed that part.
– Christopher
Nov 10 at 15:18
|
show 4 more comments
2
var products = new List<Product>();
– Klaus Gütter
Nov 10 at 14:20
2
It is declaring the type just once - the RHS is creating an instance
– Disaffected 1070452
Nov 10 at 14:23
5
Simply Java != C#.
– Daniel A. White
Nov 10 at 14:24
5
It is a limitation of the C# type inference feature. Decently explained in this blog post.
– Hans Passant
Nov 10 at 14:28
1
@Hans Passant Good point - it was not clear from the question that it is a field, not a variable.
– Klaus Gütter
Nov 10 at 14:33