What data type should I use?

I need a collection that

  • contains a set of objects associated with a binary.
  • The sequence of these pairs should be arbitrarily given by me (based on the int I get from the database) and static throughout the lifecycle.
  • The number of records will be small (0 ~ 20) but change.
  • The collection must be iterative.
  • I don't need to search for a collection for anything.
  • The double will be changed after the collection is inlated.
  • I would like to work with existing datatypes (no new classes) as it will be used in my asp.net mvc controllers, views and services, and I don't want them all to depend on the library just for this stupid owner class.

I thought that

IDictionary<int, KeyvaluePair<TheType, double>>

      

would do the trick, but then I cannot set the double after init.

- Edit -
I found out that the classes created by the linq 2 sql visual thing visual studio are actually partial classes, so you can add whatever you want to them. I solved my question by adding a double field to the partial class.
Thanks everyone for the answers you came up with.

0


source to share


4 answers


It sounds like you might just want an equivalent KeyValuePair

, but mutable. Given that you are only using it as a value pair and not a key value pair, you can simply do:

public class MutablePair<TFirst, TSecond>
{
    public TFirst First { get; set; }
    public TSecond Second { get; set; }

    public MutablePair()
    {
    }

    public MutablePair(TFirst first, TSecond second)
    {
        First = first;
        Second = second;
    }
}

      



This does not override GetHashCode or Equals because you are not actually using them (as in the value position).

+2


source


struct MyPair
{
    public object TheType;
    public double Value;
}

MyPair[] MyColleccyion = new MyPair[20]; 

      



+1


source


Well, KeyValuePair is immutable (which is good), so you have to replace the entire KeyValuePair value, not just parts of it:

yourDict[10] = new KeyValuePair<TheType, Double>(yourDict[10].Key, newValue);

      

... or think like Jon Skeet . Gah. :)

0


source


How about this

public class ListThing<TKey, TValue> : Dictionary<TKey, TValue>
{
    public double DoubleThing { get; set; }

    public ListThing(double value)
    {
        DoubleThing = value;
    }
}

      

0


source







All Articles