-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
  • 12,499
  • 17
  • 70
  • 145

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
  • 29,380
  • 18
  • 66
  • 103
1

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

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