Adding multiple values on list - c#

Is it possible to add multiple items into list or adding a list of values into a list.
here is my current pseudo code to do it:
List<string> myList = new List<string>();
myList.add("a","b","c","d","e","f","g");
myList.add("h","i","j","k","l","m","n");
myList.add("a1","a2","a3");
and my expected result is:
[["a","b","c","d","e","f","g"], ["h","i","j","k","l","m","n"], ["a1","a2","a3"]]
Any suggestions/comments TIA.

What you are asking for is a List<List<string>>. There are probably better structures for storing your data but since you haven't given any context, you can do this:
var myList = new List<List<string>>();
And add items like this:
myList.Add(new List<string> { "a", "b", "c", "d", "e", "f", "g" });
myList.Add(new List<string> { "h", "i", "j", "k", "l", "m", "n" });
myList.Add(new List<string> { "a1", "a2", "a3" });
Or in one piece of code using a collection initialiser:
var myList = new List<List<string>>
{
new List<string> { "a", "b", "c", "d", "e", "f", "g" },
new List<string> { "h", "i", "j", "k", "l", "m", "n" },
new List<string> { "a1", "a2", "a3" }
};

Should be as easy as
var myList = new List<List<string>>()
{
new List<string> { "a", "b", "c", "d", "e", "f", "g" },
new List<string> { "h", "i", "j", "k", "l", "m", "n" },
new List<string> { "a1", "a2", "a3" },
};
// OR
var myarray = new[]
{
new[] { "a", "b", "c", "d", "e", "f", "g" },
new[] { "h", "i", "j", "k", "l", "m", "n" },
new[] { "a1", "a2", "a3" },
};
Additional Resources
Object and Collection Initializers (C# Programming Guide)
C# lets you instantiate an object or collection and perform member
assignments in a single statement.
Collection initializers
Collection initializers let you specify one or more element
initializers when you initialize a collection type that implements
IEnumerable and has Add with the appropriate signature as an instance
method or an extension method. The element initializers can be a
simple value, an expression, or an object initializer. By using a
collection initializer, you do not have to specify multiple calls; the
compiler adds the calls automatically.

Related

How do I see if x is in a list automatically?

So basically, I have a list, and I have an empty string. My goal is to search if x has one of the values inside of it. Here is the code
string x = Console.ReadLine();
var strings = new List<string>() {"A", "B", "C", "D", "E", "F", "G", "H", "I","J", "K", "L", "M", "N", "O", "P", "Q","R", "S", "T", "U", "V", "W", "X", "Y","Z", "a", "b", "c", "d", "e", "f", "g", "h", "i","j", "k", "l", "m", "n", "o", "p", "q","r", "s", "t", "u", "v", "w", "x", "y","z"};
For example, if someone inputted "A B a 1" I want to see if there is a thing from the list in it (which there is) How would I do this? (C#)
I hope I understood you question. You can check if it exists using following.
var result = inputString.Split(new []{" "},StringSplitOptions.RemoveEmptyEntries)
.Any(x=>strings.Contains(x));
You need to split the input string based on whitespace, and compare if any of them exist in your original collection. You can do so using Linq

Find strings in array using C#.Net [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I built an app of Numerology based on Name.
I'm beginner using C#.net and I have trouble of functions when I input name.
It's only 1 character that the program was detected.
For Example : Jane is not contained in Arrays but only J is contained in Arrays.
I really appreciate if someone gives another simple algorithms.
string[] Array0 = { " " };
string[] Array1 = { "A", "J", "S" };
string[] Array2 = { "B", "K", "T" };
string[] Array3 = { "C", "L", "U" };
string[] Array4 = { "D", "M", "V" };
string[] Array5 = { "E", "N", "W" };
string[] Array6 = { "F", "O", "X" };
string[] Array7 = { "G", "P", "Y" };
string[] Array8 = { "H", "Q", "Z" };
string[] Array9 = { "I", "R" };
string tempName = Name.ToUpper();
foreach (string x in Array1) {
if (x.Contains(tempName))
{
Response.Write("Your name is contained in Array");
}
else {
Response.Write("Your name is Not Contained in Array");
}
}
Try this:
List<string[]> _array = new List<string[]>() { new string[] {"A", "J", "S"}, new string[] { "B", "K", "T" },new string [] {"C", "L", "U"},
new string[] {"D", "M", "V"}, new string[] { "E", "N", "W" }, new string[] { "F", "O", "X" },
new string[] { "G", "P", "Y" },new string[] { "H", "Q", "Z" }, new string[] { "I", "R" }};
bool _result = _array.Select(a => a.Any(s => Name.ToUpper().Contains(s))).FirstOrDefault();
if(_result)
Response.Write("Your name is contained in Array");
else
Response.Write("Your name is Not Contained in Array");
This code will check for any occurance of Name in array list.

How to find if an element of a list is in another list and the name of element?

First I want to know if at least one element in a first list can be found in a second list.
List<string> list1 = new[] { "A", "C", "F", "H", "I" };
List<string> list2 = new[] { "B", "D", "F", "G", "L" };
I am using below code to do this -
bool isFound = list1.Intersect(list2).Any();
But I want to know which element is that. Like in above case it is 'F'
What is the best way to do this?
You just use Intersect only:
var result = list1.Intersect(list2);
Try:
List<string> list1 = new List<string> { "A", "C", "F", "H", "I" };
List<string> list2 = new List<string> { "B", "D", "F", "G", "L" };
String sel = list1.Intersect(list2).FirstOrDefault()??"";
Console.WriteLine(sel);
Try my Demo
You can use Enumerable.Intersect method only, you don't need to use Any in your case.
Produces the set intersection of two sequences.
List<string> list1 = new List<string>(){ "A", "C", "F", "H", "I" };
List<string> list2 = new List<string>(){ "B", "D", "F", "G", "L" };
var intersect = list1.Intersect(list2);
foreach (var i in intersect)
{
Console.WriteLine(i);
}
Output will be;
F
Here is a DEMO.
Instead of bool variable You can take another list variable like:
List<string> list3 Variable to get list of items which are forund in second list and assign the result to list3
List<string> list3= list1.Intersect(list2).ToList();

Translating VB to C# - type conversion

I need help with translating some code from VB to C#.
Public Function ToBase36(ByVal IBase36 As Double) As String
Dim Base36() As String = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"}
Dim v As String
Dim i As Decimal
Do Until IBase36 < 1
i = IBase36 Mod 36
v = Base36(i) & v
IBase36 = Math.DivRem(Long.Parse(IBase36), 36, Nothing)
Loop
Return v
End Function
My problem is how type conversion works in VB, and this line gives me most trouble since IBase36 is a double, Math.DivRem() in this case should return long and Long.Parse() need string.
IBase36 = Math.DivRem(Long.Parse(IBase36), 36, Nothing)
Here is my translated, working code thanks to the JaredPar and others
public static string ToBase36(double IBase36)
{
string[] Base36 = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" };
string v = null;
long i = default(long);
while (!(IBase36 < 1))
{
IBase36 = Convert.ToDouble(Math.DivRem(Convert.ToInt64(IBase36), 36, out i));
v = Base36[i] + v;
}
return v;
}
To translate it's important to understand first how the VB.Net code functions. Under the hood it will essentially generate the following
Dim unused As Long
IBase36 = CDbl(Math.DivRem(Long.Parse(CStr(IBase36)), 36, unused)
Note: The unused variable is necessary because the third argument is an out. This is an important difference when translating to C#.
The most natural C# equivalent of the above is
long unused;
IBase36 = Convert.ToDouble(Math.DivRem(Long.Parse(IBase36.ToString()), 36, out unused);
Now try it:
long unused = null;
IBase36 = Convert.ToDouble(Math.DivRem(Convert.ToInt64(IBase36), 36, out unused));
IBase36 = (long)IBase36 / 36;
is what you want. Math.DivRem() returns the quotient which the above integer division should take care of. The third out parameter returns the remainder but since you don't care about it in vb. net code, you can just ignore that.

How to call Parallel.ForEach with a multidimensional array

I'm having a little trouble figuring out how to call the Parallel.ForEach with a 2D array of strings:
string[,] board = new string[,]{
{"A", "B", "C", "D", "E" },
{"F", "G", "H", "I", "J"},
{"K", "L", "M", "N", "O"},
{"0", "1", "2", "3", "4"}};
Parallel.ForEach(board, row =>
{
for (int i = 0; i < row.Length; ++i)
{
// find all valid sequences
}
});
If I don't specify the type explicitly I get the following error:
The type arguments for method
'System.Threading.Tasks.Parallel.ForEach(System.Collections.Generic.IEnumerable,
System.Action)' cannot be
inferred from the usage. Try
specifying the type arguments
explicitly.
What's the proper way to specify the type arguments explicitly?
The problem for you is that 2-dimensional arrays do not implement IEnumerable<one-dimensional-array>. (It does implement IEnumerable, but it's an IEnumerable of strings that "flattens" the array.) You can do two things:
Change the string[,] to a jagged array-of-arrays, string[][].
Implement your own extension method that iterates over a two-dimensional array and turns it into an IEnumerable<one-dimensional-array>.
You should still be able to make this work with a multi-dimensional array, just using Parallel.For instead of Parallel.ForEach:
string[,] board = new string[,] {
{"A", "B", "C", "D", "E" },
{"F", "G", "H", "I", "J"},
{"K", "L", "M", "N", "O"},
{"0", "1", "2", "3", "4"}
};
int height = board.GetLength(0);
int width = board.GetLength(1);
Parallel.For(0, height, y =>
{
for (int x = 0; x < width; ++x)
{
string value = board[y, x];
// do whatever you need to do here
}
}
);

Categories

Resources