C# 自定义参数

C# 自定义参数,c#,function,arguments,C#,Function,Arguments,我在c#中创建了一个非常简单的向量轴类 通常它的工作方式类似于myaxisabc=newmyaxis(p,x,y,z)(p点)和(x、y、z双倍) 但是我想用我自己的方式调用构造函数,比如 MyAxis abc = new MyAxis([0 0 0],[0 0 1]); // Looks like MatLab; 我知道我可以通过字符串操作或列表或其他方式来实现这一点,但我希望避免创建新对象并将它们传递给构造函数 我还想避免类似于。新的MyAxis(a,b,c,d,e,f)或…新的MyAxi

我在c#中创建了一个非常简单的向量轴类

通常它的工作方式类似于
myaxisabc=newmyaxis(p,x,y,z)(p点)和(x、y、z双倍)

但是我想用我自己的方式调用构造函数,比如

MyAxis abc = new MyAxis([0 0 0],[0 0 1]); // Looks like MatLab;
我知道我可以通过字符串操作或列表或其他方式来实现这一点,但我希望避免创建新对象并将它们传递给构造函数

我还想避免类似于
。新的MyAxis(a,b,c,d,e,f)
…新的MyAxis(新的点(x,y,z),…)


在C#中是否存在这种可能性?

让您的构造函数看起来像MyAxis(double[]p,double[]v)

然后可以按如下方式初始化对象:

    MyAxis abc = new MyAxis(new[] { 0, 0, 0 }, new[] { 0.1, 0.2, 1.1} );
    public MyAxis(new int[] points, new double[] doubles)
    {
     Point p = new Point(points[0], points[1], points[2]);

     double x = doubles[0], 
     y = doubles[1], 
     z = doubles[2];

     ...

     }
MyAxis abc=新的MyAxis({0,0,0},{0,0,1});//看起来几乎像MatLab


您的构造函数显然应该验证数组是否包含3个元素(或者至少元素的数量相等并且支持N维向量)

让您的构造函数看起来像
MyAxis(double[]p,double[]v)

然后可以按如下方式初始化对象:

    MyAxis abc = new MyAxis(new[] { 0, 0, 0 }, new[] { 0.1, 0.2, 1.1} );
    public MyAxis(new int[] points, new double[] doubles)
    {
     Point p = new Point(points[0], points[1], points[2]);

     double x = doubles[0], 
     y = doubles[1], 
     z = doubles[2];

     ...

     }
MyAxis abc=新的MyAxis({0,0,0},{0,0,1});//看起来几乎像MatLab


您的构造函数显然应该验证数组是否包含3个元素(或者至少验证元素的数量是否相等并支持N维向量)

您希望更改C#使用的语法,但这是不可能的。我相信最接近你的是

MyAxis abc = new MyAxis(new []{0,0,0}, new[]{0,0,1});

但这不是你想要的。不过,我认为这是你能得到的最好结果。

你想改变C#使用的语法,但这是不可能的。我相信最接近你的是

MyAxis abc = new MyAxis(new []{0,0,0}, new[]{0,0,1});

但这不是你想要的。不过,我认为这是你能得到的最好结果。

除非你传入一个双精度/整数数组,否则你对此无能为力

new MyAxis(new int[]{1, 2, 3},new int[]{1, 2, 3});
或者像你说的一个新对象,比如newpoint(x,y,z)(我知道你说你想避免这个,但是使用OO语言,它确实“起作用”)


除非您传入一个double/int数组,否则您对此无能为力

new MyAxis(new int[]{1, 2, 3},new int[]{1, 2, 3});
或者像你说的一个新对象,比如newpoint(x,y,z)(我知道你说你想避免这个,但是使用OO语言,它确实“起作用”)


那对你有用吗

public someClass( int[] input1, int[] input2 ) 

someClass temp = new someClass(new int[] { 1, 0, 1 }, new int[] { 1, 0, 1 });

那对你有用吗

public someClass( int[] input1, int[] input2 ) 

someClass temp = new someClass(new int[] { 1, 0, 1 }, new int[] { 1, 0, 1 });

您可以尝试以下方法:

    MyAxis abc = new MyAxis(new[] { 0, 0, 0 }, new[] { 0.1, 0.2, 1.1} );
    public MyAxis(new int[] points, new double[] doubles)
    {
     Point p = new Point(points[0], points[1], points[2]);

     double x = doubles[0], 
     y = doubles[1], 
     z = doubles[2];

     ...

     }
然后在MyAxis的构造函数中执行如下操作:

    MyAxis abc = new MyAxis(new[] { 0, 0, 0 }, new[] { 0.1, 0.2, 1.1} );
    public MyAxis(new int[] points, new double[] doubles)
    {
     Point p = new Point(points[0], points[1], points[2]);

     double x = doubles[0], 
     y = doubles[1], 
     z = doubles[2];

     ...

     }
如果需要,还可以创建多个构造函数

    public MyAxis(int a, int b, int c, int d, int e, int f)
    {
    }

    public MyAxis(Point a, Point b)
    {
    }

    public MyAxis(Point p, double[] doubles)
    {
    }
等等


不幸的是,无法创建自定义语法:(

您可以尝试以下方法:

    MyAxis abc = new MyAxis(new[] { 0, 0, 0 }, new[] { 0.1, 0.2, 1.1} );
    public MyAxis(new int[] points, new double[] doubles)
    {
     Point p = new Point(points[0], points[1], points[2]);

     double x = doubles[0], 
     y = doubles[1], 
     z = doubles[2];

     ...

     }
然后在MyAxis的构造函数中执行如下操作:

    MyAxis abc = new MyAxis(new[] { 0, 0, 0 }, new[] { 0.1, 0.2, 1.1} );
    public MyAxis(new int[] points, new double[] doubles)
    {
     Point p = new Point(points[0], points[1], points[2]);

     double x = doubles[0], 
     y = doubles[1], 
     z = doubles[2];

     ...

     }
如果需要,还可以创建多个构造函数

    public MyAxis(int a, int b, int c, int d, int e, int f)
    {
    }

    public MyAxis(Point a, Point b)
    {
    }

    public MyAxis(Point p, double[] doubles)
    {
    }
等等


不幸的是,没有办法创建自定义语法:(

你不需要“自定义参数”,你需要自定义语法来创建对象-答案是“否”。你不需要“自定义参数”,你需要自定义语法来创建对象-答案是“否”。这种语法不适用于我。你需要新的[]在每个传递的数组之前让编译器高兴。你可能是对的…你可以使用double[]a={0,0,1},但它只适用于这样的初始化命令。最近javascript太多了…这种语法对我不起作用。你需要新的[]你可能是对的…你可以使用double[]a={0,0,1},但它只能在这样的初始化命令上工作。最近javascript太多了。。。