All Questions
23 questions
183
votes
13
answers
543k
views
printing all contents of array in C#
I am trying to print out the contents of an array after invoking some methods which alter it, in Java I use:
System.out.print(Arrays.toString(alg.id));
how do I do this in c#?
369
votes
23
answers
515k
views
How do I concatenate two arrays in C#? [duplicate]
int[] x = new int [] { 1, 2, 3};
int[] y = new int [] { 4, 5 };
int[] z = // your answer here...
Debug.Assert(z.SequenceEqual(new int[] { 1, 2, 3, 4, 5 }));
Right now I use
int[] z = x.Concat(y)....
94
votes
8
answers
185k
views
Conversion from List<T> to array T[]
Is there a short way of converting a strongly typed List<T> to an Array of the same type, e.g.: List<MyClass> to MyClass[]?
By short i mean one method call, or at least shorter than:
...
76
votes
3
answers
29k
views
How to make IEnumerable<string>.Contains case-insensitive?
Suppose I have a .net Array of strings.
string[] strings = new string[] { "AbC", "123", "Xyz", "321" };
If I wanted to see if the array of strings contains "ABC", I could write
strings.Contains("...
17
votes
6
answers
12k
views
Getting a collection of index values using a LINQ query
Is there a better way to do this?
string[] s = {"zero", "one", "two", "three", "four", "five"};
var x =
s
.Select((a,i) => new {Value = a, Index = i})
.Where(b => b.Value.StartsWith("t"))
....
25
votes
8
answers
3k
views
Delete duplicates in a List of int arrays
having a List of int arrays like:
List<int[]> intArrList = new List<int[]>();
intArrList.Add(new int[3] { 0, 0, 0 });
intArrList.Add(new int[5] { 20, 30, 10, 4, 6 }); //this
intArrList....
22
votes
2
answers
6k
views
Why can't I use the enumerator of an array, instead of implementing it myself?
I have some code like this:
public class EffectValues : IEnumerable<object>
{
public object [ ] Values { get; set; }
public IEnumerator<object> GetEnumerator ( )
{
...
14
votes
5
answers
26k
views
Compare value to array of strings using StartsWith
I have an array:
string[] exceptions = new string[] { "one", two", "one_1", "three" };
.. I want to be able to say:
var result = from c in myCollection
where not c.Property[3].Value....
11
votes
8
answers
10k
views
Splitting an array using LINQ
I have a collection uni-dimensional like this:
[1,2,4,5.....n]
I would like to convert that collection in a bi-dimensional collection like this:
[[1,2,3],
[4,5,6],
...]
Basically I want to group ...
3
votes
2
answers
3k
views
SelectMany in Linq and dimensional array?
Why this code compiles :
byte[][] my2DArray =new byte [][]{
new byte[] {1, 2},
new byte[] {3, 4},
};
var r = my2DArray....
6
votes
3
answers
3k
views
Merge different length arrays without losing a value using Zip()
In the following code, I'm merging two arrays of types int and string. The first one's length is bigger than the second one, and as a result, the last index (which is 5) doesn't get merged:
int[] ...
4
votes
4
answers
986
views
c# Leaner way of initializing int array
Having the following code is there a leaner way of initializing the array from 1 to the number especified by a variable?
int nums=5;
int[] array= new int[nums];
for(int i=0;i<num;i++)
{
array[...
2
votes
9
answers
23k
views
Adding an element to an array
I m reading data from a source as array. a[n]
I need to add one more element to the array.
Once i get the array, i create a new array with capacity n+1 and copy all the elements to the new array and ...
24
votes
6
answers
17k
views
How do I get the differences between two string arrays in C#?
How to compare two array string using C#.net?
Eg:
string[] com1 = { "COM6", "COM7" };
string[] com2 = { "COM6", "COM7","COM8" };
Here com1 and com2 are Array string.
Result:
COM8.
How to achieve ...
18
votes
2
answers
1k
views
Enumerable.Last<T>() and C# arrays
Say I have a simple array:
double[] myDoubleArray = new double[] { 0, 1, 2, 3, 4, 5 };
Is this as performant:
double last = myDoubleArray.Last();
as this?
double last = myDoubleArray[...