小编Moo*_*eef的帖子

将struct更改为sealed类时,隐式转换失败

有问题的结构/类:

public struct HttpMethod
{
    public static readonly HttpMethod Get = new HttpMethod("GET");
    public static readonly HttpMethod Post = new HttpMethod("POST");
    public static readonly HttpMethod Put = new HttpMethod("PUT");
    public static readonly HttpMethod Patch = new HttpMethod("PATCH");
    public static readonly HttpMethod Delete = new HttpMethod("DELETE");

    private string _name;

    public HttpMethod(string name)
    {
        // validation of name
        _name = name.ToUpper();
    }

    public static implicit operator string(HttpMethod method)
    {
        return method._name;
    }

    public static implicit operator HttpMethod(string method)
    {
        return new HttpMethod(method); …
Run Code Online (Sandbox Code Playgroud)

c# struct class implicit-conversion

13
推荐指数
1
解决办法
411
查看次数

标签 统计

c# ×1

class ×1

implicit-conversion ×1

struct ×1