-1

How to reference another constructor in c#? For example

class A
{
    A(int x, int y) {}

    A(int[] point)
    {
      how to call A(point.x, point.y}?
    )
}
user1899020
  • 13,167
  • 21
  • 79
  • 154

2 Answers2

1

You can use keyword this in the "derived" constructor to call the "this" constructor:

class A
{
    A(int x, int y) {}

    A(int[] point) : this(point[0], point[1]) { //using this to refer to its own class constructor
    {

    }    
}

That aside, I think you should get the value in array by indexes: point[0], point[1] instead of doing it like getting field/property: point.x, point.y

Ian
  • 30,182
  • 19
  • 69
  • 107
1

It's pretty simple. Same way you would call a base constructor.

A(int[] point) : this(point[0], point[1])
{
}
Bauss
  • 2,767
  • 24
  • 28