[英]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.所以,如果你看到 List 的不同成员存储不同的类型。 I want to access these names in intellisense.
我想在智能感知中访问这些名称。 Like
TestSet.Sigma=0.1
.像
TestSet.Sigma=0.1
。 So that I won't mistakenly assign MisValue into SigmaValue .这样我就不会错误地将MisValue分配给SigmaValue 。 I tried to derive the class from int,but it was a sealed class.
我试图从 int 派生类,但它是一个密封类。 var also is not helping , Because I couldn't add it to dictionary.
var 也没有帮助,因为我无法将它添加到字典中。 Tuple is not available in the .net version I am using(.net 3.5).
元组在我使用的 .net 版本(.net 3.5)中不可用。 So somebody please guide me on this.
所以请有人指导我。
Note: I am using visual c# express 2008.注意:我使用的是 Visual c# express 2008。
EDIT: The company gave me "visual c# express 2008".编辑:公司给了我“visual c# express 2008”。 From the first itself this application got created using this version.The application also uses 3rd party dlls.
从第一个本身开始,这个应用程序就使用这个版本创建了。该应用程序还使用了 3rd 方 dll。 So if we change the .net version, it may affect so many things.
因此,如果我们更改.net 版本,它可能会影响很多事情。
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
.第一个是滥用列表或字典(它们是在运行时可以通过索引/键访问的任意长度结构),当您想要的是元组或简单的自定义结构时,可以在编译时通过名称访问,从而阻止错误当您想要
0
时,您不小心将索引设置为1
。
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.第二个是你想避免不小心将
Sigma
设置为MisValue
,这是一个类型问题。 Your initial approach - extending double
- makes sense, but isn't possible in .NET as you've discovered.您最初的方法 - 扩展
double
- 是有道理的,但正如您所发现的那样,在 .NET 中是不可能的。 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.这将允许您将
double
分配给它们,而不是彼此分配。 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.最后,正如评论中提到的,在 IDE 有六次重大修订和对语言有五次(我认为)的情况下使用一个 12 岁的平台是令人沮丧的方法,因为许多指南和在线资源可能会至少已更新到 C# 6。 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.
VS2019 有免费的社区版,VSCode 是免费的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.