Listing 2: C# • Create an Immutable Number Sequence

Immutable types communicate another set of design decisions to your users. They make it clear that they hold a set of data that has been calculated during construction and will never be modified.

public sealed class ImmutableNumericSequence
{
	private List<int> numberSequence = new List<int>();
	private int sum;

	public ImmutableNumericSequence()
	{
	}

	public ImmutableNumericSequence(IEnumerable<int> seeds)
	{
		foreach (int i in seeds)
		{
			numberSequence.Add(i);
			sum += i;
		}
	}

	public int Sum
	{
		get
		{
			return sum;
		}
	}
}
comments powered by Disqus
Upcoming Events

.NET Insight

Sign up for our newsletter.

I agree to this site's Privacy Policy.