O'Reilly logo

C# Quick Syntax Reference by Mikael Olsson

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

CHAPTER 10

image

Class

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 MyRectangle{  int x, y;  int GetArea() { return x * y; }}

Object creation

To use a class’s 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 MyClass{  static void Main()  {    // Create an object of MyRectangle    MyRectangle r = new MyRectangle();  }}

An object is also ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required