Skip to main content

All Questions

Tagged with
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#?
Padraic Cunningham's user avatar
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)....
hwiechers's user avatar
  • 15k
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: ...
tehvan's user avatar
  • 10.4k
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("...
Vivian River's user avatar
  • 32.3k
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")) ....
Guy's user avatar
  • 67.1k
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....
edgarmtze's user avatar
  • 25k
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 ( ) { ...
Joan Venge's user avatar
  • 330k
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....
pierre's user avatar
  • 1,245
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 ...
Rosana Huerta's user avatar
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....
Royi Namir's user avatar
  • 148k
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[] ...
first timer's user avatar
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[...
VSP's user avatar
  • 2,395
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 ...
DarthVader's user avatar
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 ...
user avatar
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[...
jonathanpeppers's user avatar

15 30 50 per page