diff --git a/ShittyLINQ/FirstOrDefault.cs b/ShittyLINQ/FirstOrDefault.cs new file mode 100644 index 0000000..afb51e2 --- /dev/null +++ b/ShittyLINQ/FirstOrDefault.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace ShittyLINQ +{ + public static partial class Extensions + { + + public static T FirstOrDefault(this IEnumerable self) + { + if (self == null) throw new ArgumentNullException(); + + var iterator = self.GetEnumerator(); + return !iterator.MoveNext() ? default(T) : iterator.Current; + } + } +} diff --git a/ShittyLinqTests/FirstOrDefaultTests.cs b/ShittyLinqTests/FirstOrDefaultTests.cs new file mode 100644 index 0000000..b3727c0 --- /dev/null +++ b/ShittyLinqTests/FirstOrDefaultTests.cs @@ -0,0 +1,40 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using ShittyLINQ; +using System; + +namespace ShittyTests +{ + + [TestClass] + public class FirstOrDefaultTests + { + [TestMethod] + public void FirstOrDefault_GetFirstNumber() + { + int[] numbers = new int[] { 1, 2, 3, 4, 5 }; + int expectedResult = 1; + + int result = numbers.First(); + + Assert.AreEqual(expectedResult, result); + } + + public void FirstOrDefaultTests_SourceIsEmpty() + { + int[] numbers = new int[] { }; + int expectedResult = 0; + + int result = numbers.First(); + Assert.AreEqual(expectedResult, result); + } + + [TestMethod] + [ExpectedException(typeof(ArgumentNullException))] + public void First_SourceIsNull() + { + int[] numbers = null; + + int result = numbers.First(); + } + } +}