pre-defined constants for non-trivial data types

后端 未结 4 1767
梦如初夏
梦如初夏 2021-01-06 18:29

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         


        
4条回答
  •  离开以前
    2021-01-06 19:11

    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 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.

提交回复
热议问题