Can't create an array of list objects - c#

I have a line of code like this:
List<string>[] apples = new List<string>()[2];
Its purpose is simply to create an array of List objects. When I try to compile my code, the above line generates this error:
Cannot implicitly convert type 'string' to 'System.Collections.Generic.List[]
I haven't been able to find much on the subject of creating an array of List objects (actually only this thread), maybe because no search engines will search for brackets.
Is the only way to create a collection of Lists to put them in another list, like below?
List<List<string>> apples = new List<List<string>>(); //I've tried this and it works as expected
Thanks for any suggestions, I'm really just curious as to why the first line of code (the List[] example) doesn't work.

You can do this. The syntax would be:
List<string>[] apples = new List<string>[2];
Note that this only allocates an array of references - you'll need to actually construct the individual list elements before you use them:
List<string>[] apples = new List<string>[2];
apples[0] = new List<string>();
apples[1] = new List<string>();
Alternatively, you can use the collection initialization syntax (works well for small numbers of fixed elements), ie:
List<string>[] apples = new[] { new List<string>(), new List<string>() };

Try this:
List<string>[] apples = new List<string>[2];
You do the initialization of each list afterwards:
apples[0] = new List<string>();

var listArray = new List<string>[2];
for (var i = 0; i < listArray.Length; i++)
{
listArray[i] = new List<string>();
}

Related

Most efficient way to compare two lists and delete the same

I want to compare two lists and get the valid words into a new list.
var words = new List<string>();
var badWords = new List<string>();
//this is just an example list. actual list does contain 700 records
words.Add("Apple");
words.Add("Moron");
words.Add("Seafood");
words.Add("Cars");
words.Add("Chicken");
words.Add("Twat");
words.Add("Watch");
words.Add("Android");
words.Add("c-sharp");
words.Add("Fool");
badWords.Add("Idiot");
badWords.Add("Retarded");
badWords.Add("Twat");
badWords.Add("Fool");
badWords.Add("Moron");
I am looking for most efficient way to compare the lists and put all the 'good' words into a new list. The finalList shouldn't contain "Moron", "Twat" and "Fool".
var finalList = new List<string>();
Or is it unnecessary to create a new List? I am happy to hear your ideas!
Thank you in advance
Use EnumerableExcept function storing in System.Linq namespace
finalList = words.Except(badWords).ToList();
Most efficient way to save your time and also the fastest way to do it, because Except implementation uses Set, which is fast
Use Enumerable.Except:
List<string> cleanList = words.Except(badWords).ToList();
This is efficient because Except uses a set based approach.
An even more efficient approach is to avoid that "bad" words are added to the first list at all. For example by using a HashSet<string> with a case-insensitive comparer:
var badWords = new HashSet<string>(StringComparer.InvariantCultureIgnoreCase){ "Idiot", "Retarded", "Twat", "Fool", "Moron" };
string word = "idiot";
if (!badWords.Contains(word))
words.Add(word);
https://msdn.microsoft.com/library/bb908822(v=vs.90).aspx
var words = new List<string>();
var badWords = new List<string>();
//this is just an example list. actual list does contain 700 records
words.Add("Apple");
words.Add("Moron");
words.Add("Seafood");
words.Add("Cars");
words.Add("Chicken");
words.Add("Twat");
words.Add("Watch");
words.Add("Android");
words.Add("c-sharp");
words.Add("Fool");
badWords.Add("Idiot");
badWords.Add("Retarded");
badWords.Add("Twat");
badWords.Add("Fool");
badWords.Add("Moron");
var result = words.Except(badWords).ToList();
Edit: Got in late.
you can use contains method
words.Where(g=>!badWords.Contains(g)).ToList()
If your don't want to create a new List you can remove the bad words from your existing List with RemoveAll()
words.RemoveAll(badWords.Contains);

How create many list as array?

I have a problem on creating a set of list in array
this is my coding but it's wrong one, any correction for this?
List<string>[] item = new List<string>[10]();
I want to create 10 list in string named as item but I can't do it
and then how can I store more than 1 element in each of the 10 item list??
item[1].add(a); //when I want to print that a I use item[1][0]
item[1].add(b); //when I want to print that b I use item[1][1]
item[2].add(aa);
item[2].add(bb);
but how can I store element in each of the list?
If you KNOW for certain that you want exactly ten lists, you can use an array instantiated to 10 items, each a list of string.
List<string> [] items = new List<string> [10];
Each List is not initialized, so you need to initialize your list before you can use it and each list can be accessed via normal indexer syntax..
if (items[0] == null)
items[0] = new List<string>();
Once initialized, you can fill in your data.
items[0].Add("another string");
If you wanted to pre-initialize each list so that you do not get a NullReferenceException, do so in a loop.
for (var i = 0; i < items.Length; i++)
items[i] = new List<string>();
However, if you think that your items may need to hold more List<string> down the road, just simply use a list of lists.
List<List<string>> items = new List<List<string>>();
List wraps array and gives you some nice syntactic sugar and optimizations for expanding arrays which makes your life a lot easier. You can still use indexer syntax to access each list within your list.
if (items[0] == null)
items[0] = new List<string>();
items[0].Add("another string").
As per your comments
"I want 10 separete list that have dynamic space"
You can define your collection as follows.
List<string> [] collection= new List<string> [10];
for(int i=0; i<10; i++)
collection[i] = new List<string>();
or, if you don't care size of the array then you can use this.
List<List<string>> collection = new List<List<string>>();

How to construct an array of strings by passing in a list?

I tried searching by "C# new string array pass dynamic" but could not find anything relevant.
int[] IDs = someMethodCall();
List<string> values = new List<string>();
foreach (int i in IDs)
{
values.Add(i.ToString());
}
someClass sc = new someClass();
sc.Value = new string[] { "values.string1", "values.string2", ... };
What I'm trying to do is to pass the strings from values to sc.Value, so I don't have to write them out (since I don't what they'll be beforehand).
sc.Value is a string[] as defined by the class I'm using from an API (not written by me).
What is the best way to do this dynamically? In other words, how to pass in dynamic values to a string[] construction?
If I'm not missing something,you can just use ToArray method
sc.Value = values.ToArray();
BTW, you don't even need to create a list in the first place:
sc.Value = someMethodCall().Select(x => x.ToString()).ToArray();
I'm a little confused by the way you word your questioning, but I think you are trying to send your list to an array, which is easily done using the code below:
List<string> values = new List<string>();
sc.Value = values.ToArray();
How about just using the built-in method ToArray:
sc.Value = values.ToArray();
Comes with List, and is an extension method for IEnumerable if you can use LINQ.

initialising multiple empty lists

Simple syntactic c# question is this.
Given this code:
List<string> Columns = new List<string>();
List<string> Parameters = new List<string>();
List<string> Values = new List<string>();
It can be reduced to:
List<string> Columns = new List<string>(), Parameters = new List<string>(), Values = new List<string>();
But can I get it shorter still, since they're all being initialised to an empty list?
Thank you all!
I can recommend to use var keyword if these variables are not class fields, because types are know from usage. It really makes no sense to flat declaration of three variables.
var Columns = new List<string>();
var Parameters = new List<string>();
var Values = new List<string>();
Yes, you can do things, like declaring multiple local variables in one line and then initializing them in one line. But please, avoid declaring multiple variables in one line - it makes code much readable.
Purely as a point of trivia/code golf:
Func<List<string>> n = () => new List<string>();
List<string> a = n(), b = n(), c = n(), d = n(), e = n(), f = n();
But it would be ridiculous to use this in place of the much clearer constructs available. It might have value if the initialization was more complex, and the code was properly named and spaced.
Func<List<string>> setupFoo = () => {
return new List<string>() { 1, 2, 3 };
};
var option1 = setupFoo();
var option2 = setupFoo();
var option3 = setupFoo();
You can't shorten anymore.
You could use some kind of "empty list factory":
public static List<string> EmptySList()
{
return EmptyList<string>();
}
public static List<T> EmptyList<T>()
{
return new List<T>();
}
...
List<string> Columns = EmptySList(), Parameters = EmptySList(), Values = EmptySList();
Honestly, you probably don't want to shorten your code. Just write something readable and maintainable:
var columns = new List<string>();
var parameters = new List<string>();
var values = new List<string>();
This is not a great practice, but if you're initializing string lists a lot within a single .cs file, you can add an alias for the class with the file's other using statements:
using StringList = List<string>;
And then the code to declare and initialize them would be:
StringList Columns = new StringList(),
Parameters = new StringList(),
Values = new StringList();
IEnumerable is the most basic interface for collections. Using this, you can instantiate any type of collection dynamically.
For empty lists, use.
Enumerable.Empty<TYPE>();
In your example...
using using System.Collections.Generic;
...
IEnumerable<string> Columns = Enumerable.Empty<string>();
IEnumerable<string> Parameters = Enumerable.Empty<string>();
IEnumerable<string> Values = Enumerable.Empty<string>();

How to cast List<> in C#

How to cast two List...
I want something like that
List<Obj1> list = new List<Obj1>();
list.add(new Obj1);
List<Obj2> list2 = new List<Obj2>();
list2.add((Obj1)list[0]);
You may be interested in the Enumerable extension method Cast.
IEnumerable<Obj2> enumerable = list.Cast<Obj2>();
You can then convert to List if necessary:
var list2 = enumerable.ToList()
(This obviously assume the cast from Obj1 to Obj2 is valid: that Obj2 derives from Obj1 or that a conversion operator exists.)
I'm not sure what you are trying to do but it might be something like this
var nums = new List<int> {3,1,4,1,5,9,2,6,5};
var words = new List<string> {"Do", "not", "disturb", "my", "circles"};
words.AddRange(nums.Cast<string>());
To add a single element, your code would work, provided you fix the syntax:
List<Obj1> list = new List<Obj1>();
list.Add(new Obj1());
List<Obj2> list2 = new List<Obj2>();
list2.Add((Obj1)list[0]);
To concatenate the the whole list you can replace the last line with list2.AddRange(list.Cast<Obj1>());

Categories

Resources