Lesson 7: Classes

We have moved onto classes. Classes is one of the final major concepts in basic C# before we are finished. So what is classes? Classes are blueprints of code, that groups together variables, methods, and events. The classes themselves do not hold any data, however, they consists of what variables and methods that defines what the object will do. Instead, classes are like custom types. In OOP, a object is an instance or a copy of a class.

For example, you might have multiple instances of a car class, and each instance is called an object. You might have a mustang object, and a civic object. While the two might have different attributes, in a class they might have a few things declared that can be used for both.

To declare a class, it is very simple. Just use the word ‘class’ followed by a name. You will need a access modifier in front of it. For example:

public class Customer
{
//Fields, properties, methods and events go here...
}

Thats is it!
A class is often used for grouping code that has a similar purpose, with the ability to be different from eachother.
A quick note is that to access something inside the class you will need to use a dot. Confused? Take the Console class for example.
The console class is a class that has the purpose of grouping code that deals with the console. Inside there are methods and variables.
To print to the console, you use the WriteLine method. To do that, we use a dot, like so:

Console.WriteLine("WORDS");

To make new objects we use the work new, like how we made new Lists. As you can see, the class name is a custom type, and the object name can be whatever. We do it in this fashion:

//if you have a class called rito
rito myObjectName = new rito();

In C# you can also inherit classes. This mean you can make a class that haves everything that another class has, but you can extend it, by added variables and methods only necessary to the extension. For example you have a employee class that has stuff for general employee info. You now need a manager class. Instead of remaking everything, just to put some manager specific variables, you can inherit classes.

public class Manager : Employee
{
    // Employee fields, properties, methods and events are inherited 
    // New Manager fields, properties, methods and events go here...
}

Finally in an class, there is a special method called the constructor. This method is automatically ran when you make a new object from a class. This is useful because you can also pass information through the parameters of a class, or preparing the object by filling it in with information and data. Here is an example:

using System;
namespace LineApplication
{
   class Line
   {
      private double length;   // Length of a line
      public Line()
      {
         Console.WriteLine("Object is being created");
      }

      public void setLength( double len )
      {
         length = len;
      }
      public double getLength()
      {
         return length;
      }

      static void Main(string[] args)
      {
         Line line = new Line();    
         // set line length
         line.setLength(6.0);
         Console.WriteLine("Length of line : {0}", line.getLength());
         Console.ReadKey();
      }
   }
}

Here is a full example of a class and object:

public class Person
{
    // Field 
    public string name;

    // Constructor that takes no arguments. 
    public Person()
    {
        name = "unknown";
    }
yolo (deny said this) teehee
    // Constructor that takes one argument. 
    public Person(string nm)
    {
        name = nm;
    }

    // Method 
    public void SetName(string newName)
    {
        name = newName;
    }
}
class TestPerson
{
    static void Main()
    {
        // Call the constructor that has no parameters.
        Person person1 = new Person();
        Console.WriteLine(person1.name);

        person1.SetName("John Smith");
        Console.WriteLine(person1.name);

        // Call the constructor that has one parameter.
        Person person2 = new Person("Sarah Jones");
        Console.WriteLine(person2.name);

        // Keep the console window open in debug mode.
        Console.WriteLine("Press any key to exit.");
        Console.ReadKey();
    }
}
// Output: 
// unknown 
// John Smith 
// Sarah Jones

In Visual Studio, when you are creating a new class, it is recommended you create a new file, to keep organized, and to speed up the workflow. To do that, in your solution explorer, right click your solution, press ADD and press class.

Please visit this link when you are done to review:
http://www.tutorialspoint.com/csharp/csharp_classes.htm

Leave a Reply

Your email address will not be published. Required fields are marked *