© The Author(s), under exclusive license to APress Media, LLC, part of Springer Nature 2022
M. OlssonC# 10 Quick Syntax Referencehttps://doi.org/10.1007/978-1-4842-7981-6_10

10. Class

Mikael Olsson1  
(1)
HAMMARLAND, Finland
 
A class is a template used to create objects. They are made up of members, the main two of which are fields and methods. Fields are variables that hold the state of the object, while methods define what the object can do.
class Rectangle
{
  int x, y;
  int GetArea() { return x * y; }
}

Object Creation

To use a class’s instance members from outside the defining class, an object of the class must first be created. This is done by using the new keyword, which will create a new object in the system’s memory.
class MyApp
{
  static void Main() ...

Get C# 10 Quick Syntax Reference: A Pocket Guide to the Language, APIs, and Library now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.