非平凡数据类型的预定义常量 [英] pre-defined constants for non-trivial data types

查看:105
本文介绍了非平凡数据类型的预定义常量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标:为同时具有ID和消息的预定义错误创建C#类. 这是我尝试过的:

My Goal: Create a C# class for predefined errors that have both an ID and a Message. Here was what I tried:

public class MyError
{
    public static readonly MyError OK = new MyError(0, "OK");
    public static readonly MyError Bad = new MyError(1, "Bad Stuff");

    public MyError(int id, string message)
    {
        this.Id = id;
        this.Message = message;
    }

    public readonly int Id;
    public readonly string Message;
}

这可以很好地编译,而且我相信它在实践中也可以正常工作.但是我总是喜欢遵循代码分析准则.在上述情况下,违反了CA2104不声明只读可变引用类型"

This compiles just fine and I am sure it would work just fine in practice. But I always like to follow Code Analysis guidelines. In the above case, CA2104 is violated "Do not declare read only mutable reference types"

除上述内容外,我还尝试了其他方法,包括尝试使用const而不是readonly(甚至不会编译),{get;私人套装; },以供成员访问,以struct代替class等.

I have tried a number of things other than the above, including trying const instead of readonly (won't even compile), { get; private set; } for member access, struct instead of class, etc.

哪个是可变类型? ID或消息?

Which is the mutable type? Id or Message?

我愿意接受任何能实现我想要的建议:非平凡数据类型的预定义常量.

I'm open to any suggestions that accomplish what I want: Pre-defined constants for non-trivial data types.

当然,我总是可以忽略该警告,但我发现通常是有原因的.

Of course, I can always just ignore the warning but I have found that there is usually a reason for them.

*澄清* 我的问题实际上不是关于特定的MyError类,而是关于为基本数据类型以外的东西定义常量的更根本的问题.

* CLARIFICATION * My question is really less about this specific MyError class and more about a more fundamental problem of defining constants for stuff other than basic data types.

假设我要定义三个常量,它们是双精度的. C#让我这样做:

Suppose I want to define three constants that are doubles. C# lets me do this like so:

   public const double HighValue = 11.0;
   public const double LowValue = 0.1;
   public const double MidValue = 5.5;

C#为我们预定义的 double 创建了少量有用的常量: NaN MinValue MaxValue NegativeInfinity PositiveInfinity .

C# creates a small handful of useful constants for double pre-defined for us: NaN, MinValue, MaxValue, NegativeInfinity, PositiveInfinity.

那么现在,如果 I 想为空间中的三维向量(X,Y,Z)预定义一些有趣的常数该怎么办?我可以为双精度数,整数,字符串等定义常量,但是如何为不那么琐碎的东西定义常量呢? Origin是一个有趣的常量,我想给它起个名字:

So now, what if I want to pre-define some interesting constants for three-dimensional vectors (X,Y,Z) in space? I can define constants for doubles, integers, strings, etc. but how do I define a constant for something less trivial? The Origin is an interesting constant I want to give a name to:

public class Vector3D
{
    /// The problem is that this does not work
    public const Vector3D Origin = { 0.0, 0.0, 0.0 };

    public double X;
    public double Y;
    public double Z;
}

我将不需要数千个常量.就像我们的朋友 double NaN 等一样,此类也不需要受到特别保护或密封.任何人都可以自由创建Vector3D对象并以他们想要的任何方式从其派生.我只想拥有一个名为Origin的特殊常量,其值为(0.0,0.0,0.0)

I'm not going to need thousands of constants. Just a few like our friend double has with NaN, etc. The class doesn't need to be especially protected or sealed either. Anybody is free to create Vector3D objects and derive from it in any way they want. I just want to have a special constant named Origin that has value (0.0, 0.0, 0.0)

推荐答案

您的类型不是可变的,但是代码分析不知道.
该警告为假阳性;你应该压制它.

Your type is not mutable, but Code Analysis doesn't know that.
This warning is a false positive; you should suppress it.

要回答您编辑过的问题:

To answer your edited question:

const仅可应用于基元.
对于自定义类型,static readonly是唯一选项.

const can only be applied to primitives.
For custom types, static readonly is the only option.

只要类型正确不可变,它就会完美地工作.

As long as the type is properly immutable, it will work perfectly.

这篇关于非平凡数据类型的预定义常量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆