public static void Test() { // TEST var numbers = (from n in Enumerable.Range(0, 6) select new { Number = n, IsOdd = n % 2 == 1 }).ToArray(); var numbersExpected = new object[] { new { Number = 0, IsOdd = false}, new { Number = 1, IsOdd = true}, new { Number = 2, IsOdd = false}, new { Number = 3, IsOdd = true}, new { Number = 4, IsOdd = false}, new { Number = 5, IsOdd = true}, }; Assert.AreDeepEqual(numbers, numbersExpected, "Range() 6 items from 0"); // TEST var repeatNumbers = Enumerable.Repeat(-3, 4).ToArray(); var repeatNumbersExpected = new[] { -3, -3, -3, -3 }; Assert.AreDeepEqual(repeatNumbers, repeatNumbersExpected, "Repeat() -3 four times"); }
static Bridge882_Static() { var a = new[] { 1, 2, 3 }; var s = 0; foreach (var v in a) { s += v; } Bridge882_Static.Sum = s; }
static Bridge882_A_Static() { var a = new[] { 5, 6, 7 }; var s = 0; foreach (var v in a) { s += v; } Bridge882_A_Static.Sum = s; }
public static void Test() { // TEST var words = new string[] { "ab2", "ac", "a", "ab12", "", "ab", "bac", "z" }; var sortedWords = (from word in words orderby word select word).ToArray(); Assert.AreDeepEqual(sortedWords, new[] { "", "a", "ab", "ab12", "ab2", "ac", "bac", "z" }, "Order by words"); // TEST var sortedWordsByLength = (from word in words orderby word.Length select word).ToArray(); Assert.AreDeepEqual(sortedWordsByLength, new[] { "", "a", "z", "ac", "ab", "ab2", "bac", "ab12" }, "Order by word length"); // TEST var sortedPersonsByName = (from p in Person.GetPersons() orderby p.Name select p.Name).ToArray(); Assert.AreDeepEqual(sortedPersonsByName, new[] { "Billy", "Dora", "Frank", "Ian", "John", "Mary", "Nemo", "Zeppa" }, "Order by person names"); // TODO test with System.StringComparison // TEST var doubles = new double[] { 1.0, -0.7, 2.1, 0.9, 1.4, 2.9 }; var sortedDoubles = (from d in doubles orderby d descending select d).ToArray(); Assert.AreDeepEqual(sortedDoubles, new[] { 2.9, 2.1, 1.4, 1.0, 0.9, -0.7 }, "Order by descending double"); // TEST var sortedPersonsByCountDesc = (from p in Person.GetPersons() orderby p.Count descending select p.Count).ToArray(); Assert.AreDeepEqual(sortedPersonsByCountDesc, new[] { 3000, 700, 700, 550, 500, 300, 100, 50 }, "Order by person count descending"); // TEST var sortedWordsByLengthAndLetters = (from word in words orderby word.Length, word select word).ToArray(); Assert.AreDeepEqual(sortedWordsByLengthAndLetters, new[] { "", "a", "z", "ab", "ac", "ab2", "bac", "ab12" }, "Order by word length then by letters"); // TEST var sortedWordsByLengthAndLettersLambda = words.OrderBy(x => x.Length).ThenByDescending(x => x).ToArray(); Assert.AreDeepEqual(sortedWordsByLengthAndLettersLambda, new[] { "", "z", "a", "ac", "ab", "bac", "ab2", "ab12" }, "Order by word length then by letters as lambda"); // TEST // var numbers = new[] { 2, 4, 6, 1, 5, 7, 9, 0, 8, 3}; var numbers = new[] { 2, 4, 6, 1, 5 }; var numbersReversed = numbers.Reverse<int>().ToArray(); Assert.AreDeepEqual(numbersReversed, new[] { 5, 1, 6, 4, 2 }, "Reverse() numbers"); }
public static void Test() { // TEST var numbers = new[] { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 }; var filteredNumbers = (from n in numbers where n <= 6 select n).ToArray(); Assert.AreDeepEqual(filteredNumbers, new[] { 5, 4, 1, 3, 6, 2, 0 }, "Where elements in integer array are below or equal 6"); // TEST var filteredCounts = (from p in Person.GetPersons() where p.Count < 501 select p.Count).ToArray(); Assert.AreDeepEqual(filteredCounts, new[] { 300, 100, 500, 50 }, "Where elements in Person array have Count below 501"); // TEST filteredCounts = (from p in Person.GetPersons() where p.Count < 501 && p.Group == "A" select p.Count).ToArray(); Assert.AreDeepEqual(filteredCounts, new[] { 300 }, "Where elements in Person array have Count below 501 ang in group 'A'"); // TEST var persons = Person.GetPersons(); var filteredPersonByCounts = (from p in Person.GetPersons() where p.Count < 501 select p).ToArray(); Assert.AreDeepEqual(filteredPersonByCounts, new[] { persons[0], persons[1], persons[3], persons[4] }, "Where elements in Person array have Count below 501. Returns Person instances"); // TEST var filteredPersonByCountAndIndex = persons.Where((p, index) => p.Count < index * 100).ToArray(); Assert.AreDeepEqual(filteredPersonByCountAndIndex, new[] { persons[4] }, "Where elements in Person array have Count meet condition (p.Count < index * 100). Returns Person instances"); }
private static void ThrowExceptionOnLast1() { var numbers = new[] { 3, 4 }; numbers.Last(x => x == 5); }
private static void ThrowExceptionOnElementAt1() { var numbers = new[] { 3, 4 }; numbers.ElementAt(3); }