Click here to Skip to main content
15,886,199 members
Articles / Programming Languages / C# 4.0

The List Trifecta, Part 2

Rate me:
Please Sign up or sign in to vote.
5.00/5 (4 votes)
7 Sep 2013LGPL310 min read 28.6K   317   12  
The BDictionary is like a Dictionary mashed up with List<T>. BList and BMultiMap also say hello.
using System;
using System.Collections.Generic;
using System.Text;

namespace Loyc.Collections
{

	/// <summary>Holds the Count property found in nearly all collection interfaces.</summary>
	/// <remarks>
	/// Microsoft has made this interface unusable by not defining it themselves in 
	/// .NET 4.5. Now that I've replaced my original interface 
	/// <code>
	///     interface ISource&lt;out T> : IEnumerable&lt;T>, ICount {}
	/// </code>
	/// with Microsoft's IReadOnlyCollection(T), the compiler complains constantly about 
	/// "Ambiguity between IReadOnlyCollection(T).Count and ICount.Count". Eliminating
	/// ICount from most places seems to be the only solution.
	/// </remarks>
	public interface ICount
	{
		/// <summary>Gets the number of items in the collection.</summary>
		int Count { get; }
	}

	/// <summary>
	/// This class contains extension methods that are provided as part of various 
	/// Loyc.Collections interfaces. For example, it provides methods such as IndexOf(),
	/// Contains() and CopyTo(), that the traditional <see cref="ICollection{T}"/> 
	/// and <see cref="IList{T}"/> interfaces require the author to write himself.
	/// </summary>
	/// <remarks>
	/// For covariant collections such as <see cref="ISource{T}"/> and <see 
	/// cref="IListSource{T}"/>, the CLR actually prohibits methods such as 
	/// Contains(T) and IndexOf(T), because T is not allowed in "input" positions. 
	/// Therefore, these extension methods must be used to fill the gap. Even
	/// methods such as <i>bool TryGet(int, out T)</i> are prohibited, so TryGet()
	/// has the signature <i>T TryGet(ref bool failed)</i> instead, and extension
	/// methods provide the original version of the method in addition.
	/// </remarks>
	public static partial class LCInterfaces
	{
		/// <summary>Returns true if the collection contains any elements.</summary>
		public static bool Any(this ICount c) { return c.Count > 0; }
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The GNU Lesser General Public License (LGPLv3)


Written By
Software Developer None
Canada Canada
Since I started programming when I was 11, I wrote the SNES emulator "SNEqr", the FastNav mapping component, the Enhanced C# programming language (in progress), the parser generator LLLPG, and LES, a syntax to help you start building programming languages, DSLs or build systems.

My overall focus is on the Language of your choice (Loyc) initiative, which is about investigating ways to improve interoperability between programming languages and putting more power in the hands of developers. I'm also seeking employment.

Comments and Discussions