Struct Example

CSharp · Console

/Program.cs

using System;

public class Program
{
  struct Book
  {
    public string title;
    public double price;
    public string author;
  }

  struct Coords
  {
    public int x;
    public int y;

    // constructor
    public Coords(int p1, int p2) {
      x = p1;
      y = p2;
    }
  }

  public static void Main()
  {
    Book myFavBook;
    myFavBook.title = "The Fellowship of the Rings";
    myFavBook.author = "J.R.R. Tolkien";
    myFavBook.price = 29.99;

    Coords point = new Coords(42, 69);

    Console.WriteLine(myFavBook.title); // outputs: "The Fellowship of the Ring"
    Console.WriteLine("x = {0}, y = {1}", point.x, point.y); // outputs: x = 42, y = 69
  }
}
;