pre-defined constants for non-trivial data types

后端 未结 4 1765
梦如初夏
梦如初夏 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

    The type is not mutable. The Code Analysis gets confused by having public readonly fields.

    You can fix the code analysis issue by making your public fields public properties:

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

提交回复
热议问题