Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/273.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将另一个结构隐式转换为我的类型?_C#_.net_Variable Assignment_Implicit - Fatal编程技术网

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

C# 如何将另一个结构隐式转换为我的类型?,c#,.net,variable-assignment,implicit,C#,.net,Variable Assignment,Implicit,因为它是MyClass x=120,是否可以创建这样的自定义类? 如果是这样,我该怎么做?不确定这是否是您想要的,但您可以通过实现隐式运算符来实现: 创建隐式运算符: 例如: 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)

因为它是
MyClass x=120,是否可以创建这样的自定义类?

如果是这样,我该怎么做?

不确定这是否是您想要的,但您可以通过实现隐式运算符来实现:
创建隐式运算符:

例如:

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

“这是个好主意吗?”这是有争议的。隐式转换倾向于打破程序员公认的标准;一般来说,这不是个好主意。但如果你正在做一些大价值的库,例如,那么它可能是一个好主意

是的,这里有一个简短的例子

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

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

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操作符,但由于一些错误,我无法实现它。现在我测试了你的代码,它可以工作了。谢谢