using System; using System.Collections; public delegate bool IntFilter(int i); public class MainClass { public static int[] FilterArrayOfInts(int[] ints, IntFilter filter) { ArrayList aList = new ArrayList(); foreach (int i in ints) { if (filter(i)) { aList.Add(i); } } return ((int[])aList.ToArray(typeof(int))); } public static void Main() { int[] nums = { 1, 2, 3, 4, 5}; int[] oddNums = FilterArrayOfInts(nums, i => ((i & 1) == 1)); foreach (int i in oddNums) Console.WriteLine(i); } }
Use First, Last with expression
using System; using System.Collections; using System.Collections.Generic; using System.Text; using System.Linq; public class MainClass{ public static void Main(){ int[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; var query = numbers.First(); Console.WriteLine("The first element in the sequence"); Console.Write(query); query = numbers.Last(); Console.WriteLine("The last element in the sequence"); Console.Write(query); Console.WriteLine("The first even element in the sequence"); query = numbers.First(n => n % 2 == 0); Console.Write(query); Console.WriteLine("The last even element in the sequence"); query = numbers.Last(n => n % 2 == 0); Console.Write(query); } }
Add Extension method
using System; using System.Collections; using System.Collections.Generic; using System.Text; using System.IO; using System.Reflection; using System.Linq; using System.Xml; using System.Xml.Linq; public static class Extensions { public static string SpaceToUnderscore(this string source) { char[] cArray = source.ToCharArray(); string result = null; foreach (char c in cArray) { if (Char.IsWhiteSpace(c)) result += "_"; else result += c; } return result; } } class Program { static void Main(string[] args) { string s = "This is a test"; Console.WriteLine(s.SpaceToUnderscore()); } }
Extension method for integer
using System;
static class MainClass {
public static int ConvertToBase(this int i, int baseToConvertTo) {
if (baseToConvertTo < 2 || baseToConvertTo > 10)
throw new ArgumentException(“Value cannot be converted to base ” + baseToConvertTo.ToString());
int result = 0;
int iterations = 0;
do {
int nextDigit = i % baseToConvertTo;
result += nextDigit * (int)Math.Pow(10, iterations);
iterations++;
i /= baseToConvertTo;
}
while (i != 0);
return result;
}
static void Main() {
try {
int x = 591;
for (int i = 2; i <= 10; i++) {
Console.WriteLine("{0} in base {1} is {2}", x, i, x.ConvertToBase(i));
}
} catch (Exception ex) {
Console.WriteLine("Exception: {0}", ex.Message);
}
}
}
[/csharp]
Use static method syntax to call the query operators.
using System; using System.Collections.Generic; using System.Linq; public class MainClass { public static void Main() { string[] names = { "J", "P", "G", "Pa" }; IEnumerable<string> filtered = Enumerable.Where(names,n => n.Contains("a")); IEnumerable<string> sorted = Enumerable.OrderBy(filtered, n => n.Length); IEnumerable<string> finalQuery = Enumerable.Select(sorted,n => n.ToUpper()); } }
Use Equal determines if two string arrays have the same elements in the same order
using System; using System.Collections.Generic; using System.Linq; using System.Text; public class MainClass { public static void Main() { var wordsA = new string[] { "c", "ap", "b" }; var wordsB = new string[] { "c", "ap", "b" }; bool match = wordsA.Equals(wordsB); Console.WriteLine("The sequences match: {0}", match); } }
One array expect another array
using System; using System.Collections; using System.Collections.Generic; using System.Text; using System.Linq; public class MainClass{ public static void Main(){ int[] numbers = {1, 2, 3, 4}; int[] numbers2 = {1, 1, 3, 3}; Console.Write(numbers.Except(numbers2)); } }