为什么静态类不允许隐式运算符?有替代方法吗?

问题描述 投票:0回答:1

不久,

我需要它,因为它更容易阅读,可以节省时间。该类是静态的,因此可以直接调用。

[string myTime = TimeFormat;直接呼叫

代替

myTime = TimeFormat.CurrentTimeFormat;

public static class TimeFormat
{
    public static implicit operator string(string x)
    {
        return "";
    }
}

错误CS0715'TimeFormat.implicit运算符字符串(string)':静态类不能包含用户定义的运算符

为什么?还有其他方法吗?

c# .net operators implicit-conversion static-classes
1个回答
-2
投票

如果您的C#版本低于v。7,则将无法使用。检查它是否已更新为最新版本!

也,here,您可以找到许多有关问题的有用信息!

© www.soinside.com 2019 - 2024. All rights reserved.