如何将另一个结构隐式转换为我的类型?

发布于 2024-09-04 17:42:43 字数 69 浏览 5 评论 0原文

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

As it is MyClass x = 120;, is it possible to create such a custom class?
If so, how can I do that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

难如初 2024-09-11 17:42:43

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

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;

It's generally considered a bad idea to use implicit operators, as they are, after all, implicit and run behind your back. Debugging code littered with operator overloads is a nightmare. That said, with something like this:

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;
    }
}

you could use:

Complex complex = 10;

or you could ever overload the + operator

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

and use code like

complex +=5;
烟花易冷人易散 2024-09-11 17:42:43

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

Not sure if this is what you want but you may get there by implementing the implicit operator:
http://msdn.microsoft.com/en-us/library/z5z9kes2(VS.71).aspx

情栀口红 2024-09-11 17:42:43

创建隐式运算符:

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 MyStruct (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);
    }
}

“这是个好主意吗?”是有争议的。隐式转换往往会违反程序员公认的标准;通常不是一个好主意。但是,例如,如果您正在创建一些大型价值库,那么这可能是一个好主意。

Create an implicit operator:

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

For example:

public struct MyStruct // I assume this is what you meant, since you mention struct in your title, but use MyClass in your example. 
{
    public MyStruct (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);
    }
}

"Is this a good idea?" is debatable. Implicit conversions tend to break accepted standards for programmers; generally not a good idea. But if you're doing some large value library, for example, then it might be a good idea.

多情癖 2024-09-11 17:42:43

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

  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); }
  }

yes, here's a short example ...

  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); }
  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文