ExactlyOne extension method I often find in codebases something on the order of if (sprockets.Count() &gt; 0) which is easily replaced with LINQ's if (sprockets.Any()). This keeps the entirety of sprockets from having to be iterated over completely (to get the count) then comparing to zero. Further, the business logic often reads something like "if there are any sprockets, inform the user of the subtotal". I also often see similar logic for exactly one of something: if (sprockets.Count() == 1) which doesn't have an easy, low-cost LINQ alternative. So I've created one here:

public static bool ExactlyOne&lt;TSource&gt;(this IEnumerable&lt;TSource&gt; source)
{
    if (source is null)
    {
        throw new ArgumentNullException(nameof(source));
    }

    using (IEnumerator&lt;TSource&gt; enumerator = source.GetEnumerator())
    {
        return enumerator.MoveNext() &amp;&amp; !enumerator.MoveNext();
    }
}


Usage is if (sprockets.ExactlyOne()) Here are unit tests. There is one helper method called Infinite() which is a never-ending enumerable, which will baffle sprockets.Count(), but not sprockets.ExactlyOne():

[TestClass]
public sealed class ExactlyOneTests
{
    [TestMethod]
    [ExpectedException(typeof(ArgumentNullException))]
    public void TestNull()
    {
        int[] nullArray = null;

        Assert.IsFalse(nullArray.ExactlyOne());
    }

    [TestMethod]
    public void TestZero()
    {
        int[] zero = Array.Empty&lt;int&gt;();

        Assert.IsFalse(zero.ExactlyOne());
    }

    [TestMethod]
    public void TestOne()
    {
        int[] one = { 1 };

        Assert.IsTrue(one.ExactlyOne());
    }

    [TestMethod]
    public void TestTwo()
    {
        int[] two = { 1, 2 };

        Assert.IsFalse(two.ExactlyOne());
    }

    [TestMethod]
    public void TestInfinite()
    {
        IEnumerable&lt;int&gt; infinite = Infinite();

        Assert.IsFalse(infinite.ExactlyOne());
    }

    private static IEnumerable&lt;int&gt; Infinite()
    {
        while (true)
        {
            yield return 0;
        }
    }
}


Looking for overall review - is the code readable, maintainable, performant. Do the tests cover the expected cases or are there more to consider?
Q&amp;A


  Is the code readable?



ExactlyOne states very clearly what the method is supposed to do.
source is null seems odd to me (does that even compile?). I'd prefer source == null. (Edit from comments: a topic about is null vs == null)
IEnumerator&lt;TSource&gt; enumerator = source.GetEnumerator() can be written as var enumerator = source.GetEnumerator().





  Is the code maintainable?



Since you are looking for a sibling function of Any&lt;T&gt;(), I would also include a ExactlyOne&lt;T&gt;(Func&lt;T, bool&gt; predicate).





  Is the code performant?



It does seem so, right? But notice that LINQ is optimized for IEnumerable&lt;T&gt; that is also ICollection&lt;T&gt;, in which case Count is used. Implementations should have an eager implementation of this property. Your method could also use this optimization Count == 1. 
I actually noticed (in my eyes) unexpected behavior in LINQ: Count&lt;T&gt;() is optimized for ICollection&lt;T&gt; but Any&lt;T() is not. This means you arguably could make a slightly faster implementation than LINQ. 
An in-depth comparison is required to find the most optimized approach. As discussed in the comments, testing the different implementations against a variety of input sources should yield us which method perfoms best under which conditions.





  Do the tests cover the expected cases or are there more to consider?


You cover null, empty, 1, multiple, early exit on infinite.. but perhaps also test on ICollection&lt;T&gt; and custom IEnumerable&lt;T&gt; implementations with eager and/or lazy loading.



Reference Source: LINQ Any vs Count

// not optimized for ICollection&lt;T&gt; (why ??)
public static bool Any&lt;TSource&gt;(this IEnumerable&lt;TSource&gt; source) {
    if (source == null) throw Error.ArgumentNull("source");
    using (IEnumerator&lt;TSource&gt; e = source.GetEnumerator()) {
        if (e.MoveNext()) return true;
    }
    return false;
}

public static bool Any&lt;TSource&gt;(this IEnumerable&lt;TSource&gt; source, Func&lt;TSource, bool&gt; predicate) {
    if (source == null) throw Error.ArgumentNull("source");
    if (predicate == null) throw Error.ArgumentNull("predicate");
    foreach (TSource element in source) {
        if (predicate(element)) return true;
    }
    return false;
}

// optimized for ICollection&lt;T&gt;
public static int Count&lt;TSource&gt;(this IEnumerable&lt;TSource&gt; source) {
    if (source == null) throw Error.ArgumentNull("source");
    ICollection&lt;TSource&gt; collectionoft = source as ICollection&lt;TSource&gt;;
    if (collectionoft != null) return collectionoft.Count;
    ICollection collection = source as ICollection;
    if (collection != null) return collection.Count;
    int count = 0;
    using (IEnumerator&lt;TSource&gt; e = source.GetEnumerator()) {
        checked {
            while (e.MoveNext()) count++;
        }
    }
    return count;
}