Classes

How do I create a class?

Classes are declared in C# using the  class  keyword. As you can see in the example below, we've declared a class called  Person .

using System; public class Person { } ...

 

We haven't given it any methods or properties just yet, we'll come to that later. So, the entire class declaration can be broken down into parts. Firstly, we have the following:

public class

This is telling the compiler that you are defining a  class  who will be  public . We'll come onto what  public  means in the next tutorial, but for now, we'll include  public  in all our class declarations. The next part of the declaration is the class name. The class name is an identifier that allows you to reference your class by type throughout your program, in this case, we've named the class  Person .

public class Person

Now this line alone is not enough to declare a valid class, we also have to add a pair of matching braces, the  {  and  }  characters:

public class Person { }

And that's it, that is a basic class with no methods or properties.

Page 3 of 20