C# generic list initialization?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • scuba264
    New Member
    • Jul 2007
    • 5

    #1

    C# generic list initialization?

    I currently have a string array that I need to perform operations on(the contains() method would be very helpful ) and I'd like to use the list structure. Is it possible to initilize a list in VS 2005 with string values in it? I can't get data in without using the add() method.

    3.0 apparently would allow:
    Code:
    List<string> stuff = new List<string> { "Entry", "Something", "Another" };
    but 2005 won't let me initalize like that. I have a lot of entries and it's silly to add() every one.

    I've tried declaring:
    Code:
    List<string>stuff = new List<string>();
    stuff = {"this", "that", "other"};
    which doesn't work either.


    Thanks for any help!
  • balabaster
    Recognized Expert Contributor
    • Mar 2007
    • 798

    #2
    Originally posted by scuba264
    I currently have a string array that I need to perform operations on(the contains() method would be very helpful ) and I'd like to use the list structure. Is it possible to initilize a list in VS 2005 with string values in it? I can't get data in without using the add() method.

    3.0 apparently would allow:
    Code:
    List<string> stuff = new List<string> { "Entry", "Something", "Another" };
    but 2005 won't let me initalize like that. I have a lot of entries and it's silly to add() every one.

    I've tried declaring:
    Code:
    List<string>stuff = new List<string>();
    stuff = {"this", "that", "other"};
    which doesn't work either.


    Thanks for any help!
    VB
    Code:
    Dim sList() As String = {"a", "b", "c"} 
    Dim oList As List(Of String)
    oList.AddRange(sList)
    C#
    Code:
    string[] sList = {"a", "b", "c"}; 
    List<string> oList = new List<string>();
    List.AddRange(sList);
    Hope that helps

    Comment

    • scuba264
      New Member
      • Jul 2007
      • 5

      #3
      Thanks for the help. I managed to get it to work also as:
      Code:
      string[] myString = {"a", "b", "c"};
      List<string> myList = new List<string> (myString);
      I'm still interested to hear if theres a way to bypass having to declare a string array and put the data directly into the list if anyone has any input.

      Thanks for the quick reply.

      Comment

      • balabaster
        Recognized Expert Contributor
        • Mar 2007
        • 798

        #4
        Originally posted by scuba264
        Thanks for the help. I managed to get it to work also as:
        Code:
        string[] myString = {"a", "b", "c"};
        List<string> myList = new List<string> (myString);
        I'm still interested to hear if theres a way to bypass having to declare a string array and put the data directly into the list if anyone has any input.

        Thanks for the quick reply.
        I figured it out:
        VB
        Code:
        Dim myList As New List(Of String)(New String() {"a","b","c"})
        C#
        Code:
        List<string> myList = new List<string>(new string[]{"a","b","c"});
        It's not quite as tidy as one might hope - but this is how to do it in one line. Sadly it still requires a string array as the new List<t> doesn't have any overloads that allow for a parameter array which means you have to create an IEnumerable (array in this instance) to pass. But at least the array is an anonymous type now which cuts down on code.

        The List<t> class only can only be instanciated with overloads of the following signatures
        new List<t>();
        new List<t>(int Capacity);
        new List<t>(System. Collections.Gen eric.IEnumerabl e(t));

        Comment

        • Plater
          Recognized Expert Expert
          • Apr 2007
          • 7872

          #5
          What about:
          Code:
          List<string> stuff = new List<string>(new string[] { "thing1", "thing2", "cat in the hat" });
          EDIT: oops, that will teach me to only read the first post before responding.

          Comment

          • balabaster
            Recognized Expert Contributor
            • Mar 2007
            • 798

            #6
            Originally posted by Plater
            What about:
            Code:
            List<string> stuff = new List<string>(new string[] { "thing1", "thing2", "cat in the hat" });
            EDIT: oops, that will teach me to only read the first post before responding.
            Better late than never Plater ;) A Dr. Seuss fan I see, hehe.

            Comment

            • r035198x
              MVP
              • Sep 2006
              • 13225

              #7
              Originally posted by scuba264
              Thanks for the help. I managed to get it to work also as:
              Code:
              string[] myString = {"a", "b", "c"};
              List<string> myList = new List<string> (myString);
              I'm still interested to hear if theres a way to bypass having to declare a string array and put the data directly into the list if anyone has any input.

              Thanks for the quick reply.
              It wouldn't be possible to have such a constructor for List.
              What would it look like? Remember it has to be generic as well.

              Comment

              • Plater
                Recognized Expert Expert
                • Apr 2007
                • 7872

                #8
                Originally posted by r035198x
                It wouldn't be possible to have such a constructor for List.
                What would it look like? Remember it has to be generic as well.
                There is, why they didn't include some form of this wrapper I don't know.
                Although I suppose making it:
                public List<object> MakeList(params object[] items)
                defeats the purpose of having a List<T>

                Code:
                public void testing()
                {
                   List<string> = MakeList("green", "eggs", "and ham");
                }
                public List<string> MakeList(params string[] items)
                {
                   return new List<string>(items);
                }

                Comment

                • r035198x
                  MVP
                  • Sep 2006
                  • 13225

                  #9
                  Originally posted by Plater
                  There is, why they didn't include some form of this wrapper I don't know.
                  Although I suppose making it:
                  public List<object> MakeList(params object[] items)
                  defeats the purpose of having a List<T>

                  Code:
                  public void testing()
                  {
                     List<string> = MakeList("green", "eggs", "and ham");
                  }
                  public List<string> MakeList(params string[] items)
                  {
                     return new List<string>(items);
                  }
                  you mean MakeList(T[] items), to make it generic? That is covered with the constructor that takes an enumerable. Making it take an array rather limits it a bit since they would need separate ones for other enumerables. It's that case of coding to the Interface rather than to the implementation again.

                  Comment

                  • Plater
                    Recognized Expert Expert
                    • Apr 2007
                    • 7872

                    #10
                    But I have a heavy dislike of non-indexed enumerables :-P

                    Comment

                    • r035198x
                      MVP
                      • Sep 2006
                      • 13225

                      #11
                      Originally posted by Plater
                      But I have a heavy dislike of non-indexed enumerables :-P
                      Don't get me started on that.
                      Anyway, I could create my own class and implement the IEnumerable interface and it would work nicely with the .NET list. That's the benefit of coding to the interface.

                      Comment

                      Working...