I have a simple C# program that contains a Coord
class that stores an integer x
and y
value. I want to treat the Coord
class like an array, as shown in the Main method. I have seen similar approaches to this in C++ and I would like to know the C# equivalent.
using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Lab6 { class Coord { public int x; public int y; } class Program { static void Main(string[] args) { Coord[] c = {{1, 2}, {5, 8}, {3, 40}, {6, 3}, {15, 12}, {1, 5}}; } } }
Answer
You have to put instances of Coord class in the Coord []
Coord[] c = new Coord[4]; c[0] = new Coord(1,3); ....
Or a better way
Coord[] c = {new Coord(1,2),new Coord(1,6)};
Dont forget to add the constructor and initialize the fields
public Coord(int x,int y){ this.x = x; this.y = y; }