简体   繁体   中英

c# - How to create list/dictionary with descriptive members

I have code which has so many dictionaries like the below `

//List[0] => AverageValue,List[1] => MeanValue, List[2] => Maximum

Dictionary<string,List<double>> TestValues ;

//List[0] => Sigma,List[1] => MisValue

Dictionary<string,List<double>> TestSet ;`

So, if you see different members of List stores different types. I want to access these names in intellisense. Like TestSet.Sigma=0.1 . So that I won't mistakenly assign MisValue into SigmaValue . I tried to derive the class from int,but it was a sealed class. var also is not helping , Because I couldn't add it to dictionary. Tuple is not available in the .net version I am using(.net 3.5). So somebody please guide me on this.

Note: I am using visual c# express 2008.

EDIT: The company gave me "visual c# express 2008". From the first itself this application got created using this version.The application also uses 3rd party dlls. So if we change the .net version, it may affect so many things.

You're mixing two different things here:

The first is abusing lists or dictionaries (which are arbitrary-length structures accessible by index/key at runtime) when what you want is a tuple or a simple custom struct, which is accessible by name, at compile-time, thus discouraging errors when you accidentally set to index 1 when you wanted 0 .

public struct TestSet
{
    double Sigma {get;set;}
    double MisValue {get;set;}
}

The second is that you want to avoid accidentally setting a Sigma into a MisValue , which is a question of types. Your initial approach - extending double - makes sense, but isn't possible in .NET as you've discovered. But you can do the same thing using wrapper types and implicit conversions:

public class Sigma
{
    public double Value {get; private set;}

    public Sigma(double value)
    {
        Value = value;
    }

    public static implicit operator double(Sigma sigma) 
    {
        return sigma.Value;
    }

    public static implicit operator Sigma(double value)
    {
       return new Sigma(value);
    }
}

And the same for the other value types. This will allow you to assign double to them, but not each other. Combine that with the custom struct above for a type-safe experience:

TestSet set = new TestSet();
set.Sigma = 5.1;               // works - implicit conversion from double.
double sigmaValue = set.Sigma; // works - implicit conversion.
Sigma anotherSigma = set.Sigma;
set.MisValue = anotherSigma;   // doesn't compile - no implicit conversion.

And finally, as mentioned in the comments, using a 12 year-old platform when there have been six major revisions to the IDE and a five (I think) to the language is a recipe for frustration, as many guides and online resources will probably have been updated to C# 6, at the very least. The only reason I can think of to use it is compatibility with existing code, and the best thing to do there is update that code. VS2019 has a free community edition and VSCode is free.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM