Results 1 to 4 of 4

Thread: [RESOLVED] Initialising a List based collection:

  1. #1

    Thread Starter
    Member
    Join Date
    Mar 2006
    Posts
    41

    Resolved [RESOLVED] Initialising a List based collection:

    Hi folks,

    The following code will compile OK:

    Code:
    //Method 1
    String[] myStrings = {"String_1", "String_2", "String_3"};
    
    List<String> myList = new List<String>(myStrings);
    But I'm wondering if it's possible to declare and initialise a List all in one line of code something like this:

    Code:
    //Method  2
    List<String> myList = new List<String>("String_1", "String_2", "String_3");
    I've looked through the documentation for working with list based collections but I didn't see any constructors or methods for doing it via method 2. I'm assuming then that it's not possible to do it that way, but If that's the case could someone give me a reason why that's not allowed?


    Thanks again for your help.
    Using: VB.net + C#.net 2005 Express Editions + .Net Framework 2.0

    ~ Man Is The Warmest Place To Hide ~

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Initialising a List based collection:

    Code:
    List<string> list = new List<string>(new string[] { "string1", "string2", "string3" });
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Member
    Join Date
    Mar 2006
    Posts
    41

    Smile Re: Initialising a List based collection:

    Thanks again jmcilhinney

    As soon as I read your answer I thought "Now why didn't I think of that!!!"


    Should I mark this post and my others as being resolved when ever I get a satisfactory answer?
    Using: VB.net + C#.net 2005 Express Editions + .Net Framework 2.0

    ~ Man Is The Warmest Place To Hide ~

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Initialising a List based collection:

    Quote Originally Posted by The Thing
    Should I mark this post and my others as being resolved when ever I get a satisfactory answer?
    Yes you should. Use the Thread Tools menu for ease.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width