473,320 Members | 1,535 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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:
Expand|Select|Wrap|Line Numbers
  1. 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:
Expand|Select|Wrap|Line Numbers
  1. List<string>stuff = new List<string>();
  2. stuff = {"this", "that", "other"};
which doesn't work either.


Thanks for any help!
Nov 6 '07 #1
10 31985
balabaster
797 Expert 512MB
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:
Expand|Select|Wrap|Line Numbers
  1. 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:
Expand|Select|Wrap|Line Numbers
  1. List<string>stuff = new List<string>();
  2. stuff = {"this", "that", "other"};
which doesn't work either.


Thanks for any help!
VB
Expand|Select|Wrap|Line Numbers
  1. Dim sList() As String = {"a", "b", "c"} 
  2. Dim oList As List(Of String)
  3. oList.AddRange(sList)
C#
Expand|Select|Wrap|Line Numbers
  1. string[] sList = {"a", "b", "c"}; 
  2. List<string> oList = new List<string>();
  3. List.AddRange(sList);
Hope that helps
Nov 6 '07 #2
Thanks for the help. I managed to get it to work also as:
Expand|Select|Wrap|Line Numbers
  1. string[] myString = {"a", "b", "c"};
  2. 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.
Nov 6 '07 #3
balabaster
797 Expert 512MB
Thanks for the help. I managed to get it to work also as:
Expand|Select|Wrap|Line Numbers
  1. string[] myString = {"a", "b", "c"};
  2. 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
Expand|Select|Wrap|Line Numbers
  1. Dim myList As New List(Of String)(New String() {"a","b","c"})
C#
Expand|Select|Wrap|Line Numbers
  1. 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.Generic.IEnumerable(t)) ;
Nov 6 '07 #4
Plater
7,872 Expert 4TB
What about:
Expand|Select|Wrap|Line Numbers
  1. List<string> stuff = new List<string>(new string[] { "thing1", "thing2", "cat in the hat" });
  2.  
EDIT: oops, that will teach me to only read the first post before responding.
Nov 6 '07 #5
balabaster
797 Expert 512MB
What about:
Expand|Select|Wrap|Line Numbers
  1. List<string> stuff = new List<string>(new string[] { "thing1", "thing2", "cat in the hat" });
  2.  
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.
Nov 6 '07 #6
r035198x
13,262 8TB
Thanks for the help. I managed to get it to work also as:
Expand|Select|Wrap|Line Numbers
  1. string[] myString = {"a", "b", "c"};
  2. 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.
Nov 7 '07 #7
Plater
7,872 Expert 4TB
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>

Expand|Select|Wrap|Line Numbers
  1. public void testing()
  2. {
  3.    List<string> = MakeList("green", "eggs", "and ham");
  4. }
  5. public List<string> MakeList(params string[] items)
  6. {
  7.    return new List<string>(items);
  8. }
  9.  
Nov 7 '07 #8
r035198x
13,262 8TB
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>

Expand|Select|Wrap|Line Numbers
  1. public void testing()
  2. {
  3.    List<string> = MakeList("green", "eggs", "and ham");
  4. }
  5. public List<string> MakeList(params string[] items)
  6. {
  7.    return new List<string>(items);
  8. }
  9.  
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.
Nov 7 '07 #9
Plater
7,872 Expert 4TB
But I have a heavy dislike of non-indexed enumerables :-P
Nov 7 '07 #10
r035198x
13,262 8TB
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.
Nov 7 '07 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: anongroupaccount | last post by:
class CustomType { public: CustomType(){_i = 0;} CustomType(int i) : _i(i) {} private: int _i; }; class MyClass
8
by: JAL | last post by:
Here is my first attempt at a deterministic collection using Generics, apologies for C#. I will try to convert to C++/cli. using System; using System.Collections.Generic; using System.Text; ...
5
by: Anders Borum | last post by:
Hello! Whilst refactoring an application, I was looking at optimizing a ModelFactory with generics. Unfortunately, the business objects created by the ModelFactory doesn't provide public...
5
by: toton | last post by:
Hi, I can initialize an array of class with a specific class as, class Test{ public: Test(int){} }; Test x = {Test(3),Test(6)}; using array initialization list. (Note Test do NOT have a...
4
by: semedao | last post by:
Hi, I want to implement list of key-values that can be sort by 2 ways. let's say that in the first step I wanted to make SortList based on Key = int index that cannot change and Value is another...
13
by: rkausch | last post by:
Hello everyone, I'm writing because I'm frustrated with the implementation of C#'s generics, and need a workaround. I come from a Java background, and am currently writing a portion of an...
2
by: Jon Slaughter | last post by:
I created a generic class and I want to pass it a enum, class GObject<SomeType> { ... public SomeType q = SomeType.Default; ... }
3
by: =?Utf-8?B?Y2hyaXNiZW4=?= | last post by:
Here are the codes Queue<string> q = new Queue<string>; Console.WriteLine(q.Count + ""); The program will crash since q is null. So my question is that what is the best way for new to...
2
by: SimonDotException | last post by:
I am trying to use reflection in a property of a base type to inspect the properties of an instance of a type which is derived from that base type, when the properties can themselves be instances of...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.