【问题标题】:How can I implicitly convert another struct to my Type?如何将另一个结构隐式转换为我的类型?
【发布时间】:2011-03-02 05:26:36
【问题描述】:

既然是MyClass x = 120;,是否可以创建这样的自定义类? 如果是这样,我该怎么做?

【问题讨论】:

    标签: c# .net variable-assignment implicit


    【解决方案1】:

    使用隐式运算符通常被认为是一个坏主意,因为它们毕竟是隐式的并且在你背后运行。调试充斥着运算符重载的代码是一场噩梦。也就是说,像这样:

    public class Complex
    {
        public int Real { get; set; }
        public int Imaginary { get; set; }
    
        public static implicit operator Complex(int value)
        {
            Complex x = new Complex();
            x.Real = value;
            return x;
        }
    }
    

    你可以使用:

    Complex complex = 10;
    

    或者你可以重载 + 操作符

    public static Complex operator +(Complex cmp, int value)
    {
      Complex x = new Complex();
      x.Real = cmp.Real + value;
      x.Imaginary = cmp.Imaginary;
      return x;
     }
    

    并使用类似的代码

    complex +=5;
    

    【讨论】:

    • 老实说,我已经阅读了 MSDN 上的 impicit 运算符,但由于一些错误我无法实现它。现在我测试你的代码并且它可以工作。谢谢
    • 为什么调试它是一场噩梦?如果您在调试时单步执行代码,该工具将像所有代码一样跳转到隐式运算符代码。
    • 是的,它会的。但是,您可能会花费数小时查看x += 1 的代码,而不会认为它实际上执行了自定义代码。
    【解决方案2】:

    不确定这是否是您想要的,但您可以通过实现隐式运算符来实现: http://msdn.microsoft.com/en-us/library/z5z9kes2(VS.71).aspx

    【讨论】:

      【解决方案3】:

      创建隐式运算符:

      http://msdn.microsoft.com/en-us/library/z5z9kes2.aspx

      例如:

      public struct MyStruct // I assume this is what you meant, since you mention struct in your title, but use MyClass in your example. 
      {
          public MyClass(int i) { val = i; }
          public int val;
          // ...other members
      
          // User-defined conversion from MyStruct to double
          public static implicit operator int(MyStruct i)
          {
              return i.val;
          }
          //  User-defined conversion from double to Digit
          public static implicit operator MyStruct(int i)
          {
              return new MyStruct(i);
          }
      }
      

      “这是个好主意吗?”值得商榷。隐式转换往往会破坏程序员公认的标准;通常不是一个好主意。但是,例如,如果您正在做一些大型价值库,那么这可能是一个好主意。

      【讨论】:

        【解决方案4】:

        是的,这是一个简短的例子......

          public struct MyCustomInteger
          {
             private int val;
             private bool isDef;
             public bool HasValue { get { return isDef; } } 
             public int Value { return val; } } 
             private MyCustomInteger() { }
             private MyCustomInteger(int intVal)
             { val = intVal; isDef = true; }
             public static MyCustomInteger Make(int intVal)
             { return new MyCustomInteger(intVal); }
             public static NullInt = new MyCustomInteger();
        
             public static explicit operator int (MyCustomInteger val)
               { if (!HasValue) throw new ArgumentNullEception();
                 return Value; }
             public static implicit operator MyCustomInteger (int val)
               {  return new MyCustomInteger(val); }
          }
        

        【讨论】:

        • 你的意思是implicit 而不是explicit
        • 哪个?两者都在那里...implicit 是在没有机会施放失败时...explicit 是在它可能失败时...
        • 啊,我的错。我原以为两者都是implicit,如果你可以在不检查的情况下双向转换的话。
        猜你喜欢
        • 2011-03-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-04-20
        • 2021-04-16
        • 1970-01-01
        • 2011-04-08
        相关资源
        最近更新 更多