【问题标题】:Name of pattern and interface for bidirectional implicit operator overload双向隐式运算符重载的模式和接口名称
【发布时间】:2011-11-17 21:47:20
【问题描述】:

我最近发现了 C# 中隐式运算符重载的奇妙之处。我想知道,如果您有两种隐式运算符重载的“方式”,例如:

public static implicit operator FooType(int num)
{
    return new FooType(num);
}

public static implicit operator int(FooType fooType)
{
    return fooType.IntValue;
}
  1. 该设计模式有名称吗?
  2. 是否有我可以使用的预定义 .NET 接口,例如 ICastable<int>

【问题讨论】:

    标签: c# .net design-patterns interface operator-overloading


    【解决方案1】:
    1. 我不知道。

    2. 您可以考虑实施IConvertible。 (如果你的类也可以比较事物,你可以考虑实现IComparable<T>。)

    【讨论】:

      【解决方案2】:
      1. 如果是我从来没听说过
      2. 运算符是静态结构,在编译时被解析。它们不能成为任何界面的一部分

      【讨论】:

      • 不反对@Eric,但你的回答首先出现了,你让我知道静态不允许用于接口。所以,谢谢你们俩。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-09
      • 1970-01-01
      • 2014-08-18
      相关资源
      最近更新 更多