Click here to Skip to main content
11,429,977 members (75,581 online)
Click here to Skip to main content

Tagged as

Use Collection Object Initializers to Add Items

, 29 Apr 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
Use of Collection Object initializers instead of adding into a collection explicitly
Problem: If we wanted to create a list which contains a collection of strings, we need to do something like this with C# 2.0:

// Creating collection of string
     List<string> stringCollection = new List<string>();
     stringCollection.Add("The");
     stringCollection.Add("Code");
     stringCollection.Add("Project");

Solution: Now in C# 3.0, we do in the following way:

IEnumerable<string> stringCollections = new List<string>
     {
          "The",
          "Code",
          "Project"
     };

This isn't a revolutionary idea, but I think it's very useful, simple and it costs less performance hits than the old one.

I advise to use the latest one.

Thanks Smile | :)

License

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

Share

About the Author

Sanjay J Patolia
Software Developer Software Industry
India India
No Biography provided

Comments and Discussions

 
QuestionWhy should i use IEnumerable.. Pin
V G S Naidu A29-Oct-12 0:38
memberV G S Naidu A29-Oct-12 0:38 
AnswerRe: Why should i use IEnumerable.. Pin
Sanjay J Patolia19-Feb-13 0:31
memberSanjay J Patolia19-Feb-13 0:31 
GeneralRe: A method which accepts a list, can only accept a list, where... Pin
Patolia Sanjay2-May-11 22:31
memberPatolia Sanjay2-May-11 22:31 
Generalhow do i implement this in my own classes, so they would sup... Pin
mastamac5-May-11 12:16
membermastamac5-May-11 12:16 
GeneralYou are talking about static cases. This works only when you... Pin
ben pour3-May-11 10:52
memberben pour3-May-11 10:52 
Generalso which i should use in my code? Pin
spydeehunk2-May-11 23:47
memberspydeehunk2-May-11 23:47 
GeneralIList<string> namelist = new List<string> { "sunny","kumar",... Pin
spydeehunk2-May-11 21:54
memberspydeehunk2-May-11 21:54 
GeneralRe: A method that accepts a List can only accept a List (or some... Pin
Patolia Sanjay2-May-11 22:30
memberPatolia Sanjay2-May-11 22:30 
Generalwhat is the use of IEnumerable here because if i want to hav... Pin
spydeehunk2-May-11 19:13
memberspydeehunk2-May-11 19:13 
GeneralRe: String is just an example here, we can collect reference typ... Pin
Patolia Sanjay2-May-11 19:16
memberPatolia Sanjay2-May-11 19:16 
GeneralPerformance Pin
kornman0028-Apr-11 8:36
memberkornman0028-Apr-11 8:36 
GeneralRe: Performance Pin
Patolia Sanjay2-May-11 19:17
memberPatolia Sanjay2-May-11 19:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150428.2 | Last Updated 29 Apr 2011
Article Copyright 2011 by Sanjay J Patolia
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid