Click here to Skip to main content
15,891,607 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a beginner question.
I'm used to seeing a list created with the following syntax:
List<Employee> emp = new List<Employee>();

but I recently saw an List defined like this:
List<Employee><employee> emp = new List<Employee><employee>

What is the purpose of the second pair of angled brackets?

Thanks,
C. T.

What I have tried:

What I specified above ... simple question about C# functionality
Posted
Updated 23-Mar-19 10:32am
Comments
Maciej Los 23-Mar-19 15:00pm    
Are you sure that second brackets aren't circle brackets?
Charles T. Blankenship 23-Mar-19 15:09pm    
Yes sir ... positive ... I got it from an article here on CodeProject ... here is the link:

https://www.codeproject.com/Articles/897559/Learn-MVC-in-days-Day
Charles T. Blankenship 23-Mar-19 15:16pm    
I'm pretty sure it is invalid C# syntax ... but just wanted to make sure C# didn't introduce something I didn't know about.

It is indeed invalid syntax. What happened is that the author of the article did not escape brackets while writing its code-block; the system considered they were unclosed tags and tried to "correct" the mistake, leading to an incorrect syntax. Obviously the author did not notice the fact. By reading the comments to the article, you will find that several users have reported the issue.
 
Share this answer
 
A List<T> Class (System.Collections.Generic) | Microsoft Docs[^] does not have such of constructor[^].

There's only one constructor which accepts circle brackets: List<T>(IEnumerable<T>). See:

C#
void Main()
{
	
	
	string[] dinos = {"Tyrannosaurus",
	        "Amargasaurus",
	        "Mamenchisaurus",
	        "Deinonychus",
	        "Compsognathus"};
	
	List<Dinosaur> dinosaurs = new List<Dinosaur>(dinos.Select(x=>new Dinosaur(){Name=x}));
	//dinosaurs list has been initiated and set
	
			
}

// Define other methods and classes here
public class Dinosaur
{
	public string Name = string.Empty;

}


So, it's a bug in referenced article.
 
Share this answer
 
Comments
BillWoodruff 24-Mar-19 5:29am    
+5 Hi, call me a fanatic: I always insist my students use new[] :)

This will work too:

List<dinosaur> dinolist = dinos.Select(dino => new Dinosaur() { Name = dino }).ToList();
Maciej Los 24-Mar-19 5:44am    
Hi, Fanatic ;)
Thank you very much, Bill.
Cheers
Maciej

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