Boris Callens
Boris Callens

Reputation: 93307

What datatype to use?

I need a collection that

I thought

IDictionary<int, KeyvaluePair<TheType, double>>

would do the trick, but then I can't set the double after init.

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

Upvotes: 0

Views: 658

Answers (4)

Jon Skeet
Jon Skeet

Reputation: 1499880

It sounds like you may just want an equivalent of KeyValuePair, but mutable. Given that you're only using it as a pair of values rather than a key-value pair, you could just 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 doesn't override GetHashCode or Equals, because you're not actually using those (as it's in a value position).

Upvotes: 2

Hath
Hath

Reputation: 12769

How about this

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

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

Upvotes: 0

James Curran
James Curran

Reputation: 103485

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

MyPair[] MyColleccyion = new MyPair[20]; 

Upvotes: 1

Lasse V. Karlsen
Lasse V. Karlsen

Reputation: 391296

Well, KeyValuePair is immutable (which is a good thing), so you'll have to replace the entire value of KeyValuePair, not just the part of it:

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

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

Upvotes: 0

Related Questions