Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions ShittyLINQ/FirstOrDefault.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace ShittyLINQ
{
public static partial class Extensions
{

public static T FirstOrDefault<T>(this IEnumerable<T> self)
{
if (self == null) throw new ArgumentNullException();

var iterator = self.GetEnumerator();
return !iterator.MoveNext() ? default(T) : iterator.Current;
}
}
}
40 changes: 40 additions & 0 deletions ShittyLinqTests/FirstOrDefaultTests.cs
Original file line number Diff line number Diff line change
@@ -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();
}
}
}