C# Tutorial - Classes

posted Jun 26, 2011, 11:07 PM by magic stone

A class is an abstract representation for some particular type of object. It can be described as a template or blueprint for an object, as opposed to the actual object itself. Thus, objects are an instance of a class - they come into existence at some specific time, persist for some duration, and then disappear when they are no longer needed. Classes are the abstract descriptions used by the system to create objects when called upon to do so.

A class contains attributes (information) and methods (behaviour) that act upon the attributes. So what does a class look like? If we look at our person object, we can use this to create an example class.

class Person
{
  . . .
}
We declare our class using the keyword class, followed by the name of our class, which in this case is Person. We then use the curly braces ({}) to show the start and end of the class, which also defines the scope of the data used in the class. What this means is that all attributes and methods associated with the class must be between the two curly braces.

Attributes

The data located within a class are referred to as variables or attributes. They can be of any data type including other classes.

class Person
{
  // Attributes
  private string _forename;
  private string _surname;
}

An attribute is always declared in the following manner:

[access-modifier] data-type attribute-name;

Where:

  • The access-modifier is optional. If you do not define one, then the attribute is defaulted to private.

  • The data-type is the type of object that we want our attribute to be, such as a string or integer.

  • The attribute-name is whatever we want to refer to the object. Remember that C# is a case sensitive language, so the attributes Forename and forename do not refer to the same object.

Each attribute definition is terminated with a semicolon (;).

Methods

Methods are functions that manipulate the data associated with an object or perform some other operation relevant to the object. A method can be thought of as a named sequence of statements. So let us add two methods to our Person class.

class Person
{
  // Attributes
  private string forename;
  private string surname;
  . . . 

  // Methods
  public void setSurname( string aSurname )
  {
    surname = aSurname;
  }

  public string getSurname()
  {
    return surname;
  }
}

A method is always declared in the following manner:

[access-modifier] return-type method-name( [parameter-list] )
{
  method-body-statements;
}

Where:

  • The access-modifier is optional. If you do not define one, then the method is defaulted to private.

  • The return-type is the name of a type and specifies what kind of information the method returns. This can be the name of any type, such as int or string. If you are writing a method that does not return a value, you must use the keyword void in place of the return-type.

  • The method-name is the name used to call the method. Method names must follow the same identifier rules as variable names. For example, getSurname is a valid method name, whereas get$Surname is not valid. I use camelCase for method names, as well as making them descriptive - such as starting with a verb, as in getSurname.

  • The parameter-list is optional and describes the types and names of the information that the method accepts. You write the parameters between the left and right brackets as though you're declaring variables: name of the type, followed by the name of the parameter. If the method you're writing has two or more parameters, you must separate them with commas.

  • The method-body-statements are the lines of code that are run when the method is called. They are enclosed in an opening curly brace ({) and a closing curly brace (}).

Comments