This is from Reflector:
public static TSource SingleOrDefault(this IEnumerable source)
{
if (source == null)
{
throw Error.ArgumentNull("source");
}
IList list = source as IList;
if (list != null)
{
switch (list.Count)
{
case 0:
return default(TSource);
case 1:
return list[0];
}
}
else
{
using (IEnumerator enumerator = source.GetEnumerator())
{
if (!enumerator.MoveNext())
{
return default(TSource);
}
TSource current = enumerator.Current;
if (!enumerator.MoveNext())
{
return current;
}
}
}
throw Error.MoreThanOneElement();
}
It should throw exception if there're more than one element.