【发布时间】:2021-05-16 09:38:24
【问题描述】:
考虑以下几点:
public sealed class RequestType
{
private RequestType()
{
}
private RequestType(string value)
{
Value = value;
}
private string Value { get; }
public static RequestType Get => new RequestType("GET");
public static RequestType Post => new RequestType("POST");
public static RequestType Put => new RequestType("PUT");
public static RequestType Head => new RequestType("HEAD");
public static RequestType Delete => new RequestType("DELETE");
public static RequestType Patch => new RequestType("PATCH");
public static RequestType Options => new RequestType("OPTIONS");
....
public static bool operator ==(RequestType r, string o)
{
return r.Equals(o.ToUpperInvariant());
}
....
}
我在RequestType 和object 上都收到了Possible NullReferenceException。 object 是 null 的可能性是正确的,但它不适用于 RequestType,因为所有属性都已经预定义并且显然无法实例化它。
那么,在这种情况下应该采取什么正确的行动?无论如何检查 null 是一种好习惯吗?
【问题讨论】: