Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I want to create a immutable Scale class in C#.

public sealed class Scale
{
    string _Name;
    string _Description;
    SomeOrderedCollection _ScaleValueDefinitions;
    Unit _Unit

    // properties
    ....
    // methods
    ContainsValue(double value)
    ....

    // constructors
    // all parameters except scalevaluedefinitions are optional
    // for a Scale to be useful atleast 1 ScaleValueDefinition should exist
    public Scale(string name, string description, SomeOrderedCollection scaleValueDefinitions, unit)
    { /* initialize */}
}

so first a ScaleValueDefinition should be represented by to values:

  • Value (double)
  • Definition (string)

these values are known before the Scale class is created and should be unique. so what is the best approach.

  1. create a immutable class ScaleValueDefinition with value and definition as properties and use it in a list.
  2. use a dictionary.
  3. use another way i didn't think of...

and how to implement it. for option 1. i can use params ScaleValueDefinition[] ValueDefinitions in the constructor, but how to do it for the other options?

and as last at what amount of value's (properties) should i choose one option over the other?

share|improve this question

migrated from stackoverflow.com Oct 18 '13 at 1:08

This question came from our site for professional and enthusiast programmers.

    
For option 1, a Tuple<double,string> is an immutable type already –  RedPolygon Oct 8 '13 at 15:08

3 Answers 3

up vote 2 down vote accepted

like this maybe?

public sealed class Scale<TUnit> : IReadOnlyList<TUnit>
{
    private readonly string name;
    private readonly string description;
    private readonly IList<TUnit> definition;

    public Scale(
             string name,
             string description,
             IEnumerable<TUnit> definition) : this(name, description)
    {
        this.definition = definition.ToList();
    }

    public Scale(
             string name,
             string description,
             params TUnit[] definition) : this(name, description)
    {
        this.definition = definition;
    }

    private Scale(
             string name,
             string description)
    {
        this.name = name;
        this.description = description;
    }


    public string Name
    {
        get { return this.name; }
    }

    public string Description
    {
        get { return this.description; }
    }

    public TUnit this[int index]
    {
        get { return this.definition[index]; }
    }

    public int Count
    {
        get { return this.definition.Count; )
    }

    public IEnumerator<TUnit> GetEnumerator()
    {
        return this.definition.GetEnumerator();
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        return this.GetEnumerator();
    }
}
share|improve this answer
    
looks good. even when Unit isn't the class in the collection ScaleValueDefinition is. gonna try it out now. –  user2038134 Oct 8 '13 at 17:44
    
I really like you answer. the only down side is that i still use vs2010. is there an alternative to IReadOnlyList? –  user2038134 Oct 8 '13 at 17:57
    
@user2038134, it sounds dirty but the standard approach was to implement IList instead and throw NotImplementedExceptions on the members that you do not wish to support. There is a question and answer about it somewhere. –  Jodrell Oct 9 '13 at 8:39

It's rather opinion based but explicit classes seem to be better as they really express your intent. Also, code is easier to maintain - imagine changing keys of your dictionary all over the code, whereas for a class one place refactoring would suffice.

share|improve this answer

You haven't mentioned whether you will need to look up definitions by their value, which of course is the main purpose of a dictionary. Otherwise, I don't see any reason to use a dictionary.

If you need to do lookups, I would actually use both: that is, a Dictionary<double, ScaleValueDefinition>. That way, I can look up the definitions by the value, but also retain a reference to the value I looked up. This also means the dictionary can be dynamically generated from a sequence of ScaleValueDefinitions (using ToDictionary), and the order of that sequence can be stored, without forcing the user to pass in an OrderedDictionary:

private IEnumerable<ScaleValueDefinition> _internalOrderedCollection;
private Dictionary<double, ScaleValueDefinition> _internalDictionary;

public Scale(string name, string description, IEnumerable<ScaleValueDefinition> scaleValueDefinitions) {
    _name = name;
    _description = description;
    _internalOrderedCollection = scaleValueDefinitions.ToList();
    _internalDictionary = scaleValueDefinitions.ToDictionary(svd => svd.Value);
}

For immutability, you can simply wrap the internal dictionary in a public ReadOnlyDictionary.

share|improve this answer
    
yes, it will be used to get a definitions based on a value. but why should i keep the internalOrderedCollection if my class is immutable. –  user2038134 Oct 8 '13 at 18:01
    
@user2038134 I assumed you wanted to keep the order intact, since you had SomeOrderedCollection in your example code. The internal list would allow you to iterate the definitions in the same order they were passed in. It wouldn't be public, although you could expose a ReadOnlyCollection of it. –  nmclean Oct 8 '13 at 18:15
    
i think we have a different view on SomeOrderedCollection. what i meant with it was something like SortedSet<t> or SortedList<tk,tv> –  user2038134 Oct 8 '13 at 19:13

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.