Click here to Skip to main content
15,881,757 members
Articles / Programming Languages / C#
Tip/Trick

Use Collection Object Initializers to Add Items

Rate me:
Please Sign up or sign in to vote.
3.00/5 (4 votes)
29 Apr 2011CPOL 18.1K   3   12
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:

XML
// 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:

XML
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 :)

License

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


Written By
Software Developer Software Industry
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionWhy should i use IEnumerable.. Pin
V G S Naidu A28-Oct-12 23:38
V G S Naidu A28-Oct-12 23:38 
AnswerRe: Why should i use IEnumerable.. Pin
Sanjay J Patolia18-Feb-13 23:31
Sanjay J Patolia18-Feb-13 23:31 
GeneralRe: A method which accepts a list, can only accept a list, where... Pin
Sanjay J Patolia2-May-11 21:31
Sanjay J Patolia2-May-11 21:31 
Generalhow do i implement this in my own classes, so they would sup... Pin
captnmac5-May-11 11:16
captnmac5-May-11 11:16 
GeneralYou are talking about static cases. This works only when you... Pin
Behzad Talebpour3-May-11 9:52
Behzad Talebpour3-May-11 9:52 
Generalso which i should use in my code? Pin
spydeehunk2-May-11 22:47
spydeehunk2-May-11 22:47 
GeneralIList<string> namelist = new List<string> { "sunny","kumar",... Pin
spydeehunk2-May-11 20:54
spydeehunk2-May-11 20:54 
GeneralRe: A method that accepts a List can only accept a List (or some... Pin
Sanjay J Patolia2-May-11 21:30
Sanjay J Patolia2-May-11 21:30 
Generalwhat is the use of IEnumerable here because if i want to hav... Pin
spydeehunk2-May-11 18:13
spydeehunk2-May-11 18:13 
GeneralRe: String is just an example here, we can collect reference typ... Pin
Sanjay J Patolia2-May-11 18:16
Sanjay J Patolia2-May-11 18:16 
GeneralPerformance Pin
kornman0028-Apr-11 7:36
kornman0028-Apr-11 7:36 
GeneralRe: Performance Pin
Sanjay J Patolia2-May-11 18:17
Sanjay J Patolia2-May-11 18:17 

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

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