Until today it was not possible to often encounter overload, I ask for help with this.

There is a method:

public char GetFigureAt(int x, int y) { Square square = new Square(x ,y); Figure f = board.GetFigureAt(square); return f == Figure.none ? '.' : (char)f; } 

For two coordinates it works without problems, but when I need to enter only one value, an error appears that I need to specify 2 parameters and not 1.

 string figure = chess.GetFigureAt(from).ToString(); 

Actually the question is how to overload correctly this method that would work with 1 value?

  • One way is to create the same method and specify only one coordinate in the input parameters. You can also use optional parameters, but then one of the coordinates will always have a default value (unless you override it) - alladuh
  • tried to write a method that takes one value of the type you need? - tym32167
  • @alladuh can be more detailed about the optional parameters? - jocker64
  • one

1 answer 1

An example of overloading the GetFigureAt method from a question:

 public char GetFigureAt(Point point) { return GetFigureAt(point.X, point.Y) } public char GetFigureAt(int x, int y) { var square = new Square(x, y); var figure = board.GetFigureAt(square); return figure == Figure.none ? '.' : (char)figure; }