Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/270.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/clojure/3.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#_Arrays_Operators_Overloading - Fatal编程技术网

C# 如何初始化这样的自定义类?

C# 如何初始化这样的自定义类?,c#,arrays,operators,overloading,C#,Arrays,Operators,Overloading,C:如何初始化自定义类,如下所示 Point3 Last = { 0, 0, 0 }; 我意识到我可以重载赋值运算符“=,”,但我希望能够这样赋值: Point3 p1 = { 3, 4, 5 }; Point3 p2 = p1; public class Point3 { public static implicit operator Point3(int[] ints) { return new Point3(); } } 所以我不确定重载赋值运

C:如何初始化自定义类,如下所示

Point3 Last = { 0, 0, 0 };
我意识到我可以重载赋值运算符“=,”,但我希望能够这样赋值:

Point3 p1 = { 3, 4, 5 };
Point3 p2 = p1;
public class Point3
{
    public static implicit operator Point3(int[] ints)
    {
        return new Point3();
    }
}
所以我不确定重载赋值运算符是否有帮助

我的看法如下:

public static Point3 operator =(Point3 P, double[] Vs)
    {
        return new Point3(Vs[0], Vs[1], Vs[2]);
    }

有任何指针吗?

您可以创建如下隐式运算符:

Point3 p1 = { 3, 4, 5 };
Point3 p2 = p1;
public class Point3
{
    public static implicit operator Point3(int[] ints)
    {
        return new Point3();
    }
}
可以使用以下命令调用:

Point3 p1 = new[] { 1, 2, 3 };
请注意,您需要显式地创建数组,并且不能使用初始值设定项表达式。基本上,您要做的是解释右侧,并在右侧和自定义类型之间提供隐式转换。然后,您可以定义如何准确地处理此转换


可以创建如下所示的隐式运算符:

Point3 p1 = { 3, 4, 5 };
Point3 p2 = p1;
public class Point3
{
    public static implicit operator Point3(int[] ints)
    {
        return new Point3();
    }
}
可以使用以下命令调用:

Point3 p1 = new[] { 1, 2, 3 };
请注意,您需要显式地创建数组,并且不能使用初始值设定项表达式。基本上,您要做的是解释右侧,并在右侧和自定义类型之间提供隐式转换。然后,您可以定义如何准确地处理此转换


在点3和int[]之间创建隐式转换。不过,您可能需要使用Point3 p1=new[]{3,4,5},除非Point3实现IEnumerable,否则我认为您无法满足您的要求。您可以执行其他操作,如Point3 Last=new[]{0,0,0},然后从数组中执行隐式转换运算符。请注意,赋值运算符不能重载,请参阅本文在Point3和int[]之间创建隐式转换。不过,您可能需要使用Point3 p1=new[]{3,4,5},除非Point3实现IEnumerable,否则我认为您无法满足您的要求。您可能还可以执行其他操作,例如Point3 Last=new[]{0,0,0},然后是数组中的隐式转换运算符