Click here to Skip to main content
15,885,366 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;
using System.Linq;

namespace Loyc.Collections
{
	/// <summary>Helps you implement sources (read-only collections) by providing
	/// default implementations for most methods of <see cref="ICollection{T}"/> and
	/// <see cref="IReadOnlyCollection{T}"/>.</summary>
	/// <remarks>
	/// You only need to implement two methods yourself:
	/// <code>
	///     public abstract int Count { get; }
	///     public abstract Iterator&lt;T> GetIterator();
	/// </code>
	/// </remarks>
	[Serializable]
	public abstract class SourceBase<T> : IReadOnlyCollection<T>, ICollection<T>
	{
		#region ISource<T> Members

		public abstract int Count { get; }
		public abstract IEnumerator<T> GetEnumerator();
		System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return GetEnumerator(); }

		#endregion

		#region ICollection<T> Members

		void ICollection<T>.Add(T item)
		{
			throw new NotSupportedException("Collection is read-only.");
		}
		void ICollection<T>.Clear()
		{
			throw new NotSupportedException("Collection is read-only.");
		}
		void ICollection<T>.CopyTo(T[] array, int arrayIndex)
		{
			ListExt.CopyTo(this, array, arrayIndex);
		}
		bool ICollection<T>.IsReadOnly
		{
			get { return true; }
		}
		bool ICollection<T>.Remove(T item)
		{
			throw new NotSupportedException("Collection is read-only.");
		}
		public bool Contains(T item)
		{
			return Enumerable.Contains(this, item);
		}

		#endregion
	}
}

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