Home

Introduction to Classes

 

Introduction

To use a variable, you can declare it using a known and simple data type. For example, you can use an integer to declare a variable that represents the number of bedrooms of a house. Here is an example:

using System;

class Program
{
    static void Main()
    {
        int bedrooms = 3;
    }
}

As opposed to a simple variable, in C#, you can use one or more variables to create a more complete or complex object.

HomePractical Learning: Introducing Classes

  1. Start Microsoft Visual C#
  2. Create a Console Application named DepartmentStore1
  3. Change the file as follows:
     
    using System;
    
    class Exercise
    {
        static void Main()
        {
        }
    }
  4. Execute the application to see the result
  5. Close the DOS window

Creating a Class

A class is a technique of using one or a group of variables to be used as a foundation for a more detailed variable. To create a class, you start with the class keyword followed by a name and its body delimited by curly brackets. Here is an example of a class called House:

class House
{
}

A class is created in a code file. As such, you can include it in the first file of your project. Here is an example:

using System;

class House
{
}

class Program
{
    static void Main()
    {
        int bedrooms = 3;
    }
}

You can also create a class in its own file. To assist you with this, Microsoft Visual C# 2005 provides a wizard. To use it, on the main menu, you can click Project -> Add Class... or Project -> Add New Item... In the Templates list, click Class. In the Name text box, accept or change the default name and click Add. A new file named after the class with the .cs extension would be added to your project.

When a project is made of various files, each file is represented by a tab in the top section of the Code Editor:

To access a file, you can click its tab.

HomePractical Learning: Introducing Classes

  1. To create a new class, on the main menu, click Project -> Add Class...
  2. In the Add New Item dialog box, change the name to DepartmentStore and click Add

Visually Managing Classes

To assist you with managing the various classes of a project, Microsoft Visual C# 2005 includes various tools and windows. One of the windows you can use is called the Class View. To display it, on the main menu, you can click View -> Class View:

The Class is made of four sections. The toolbar under the title bar includes four buttons. To identify a button, you can position the mouse on it and a tool tip would appear.

Under the toolbar, another bar made of a combo box and a button allow you to search.

The main top section of the Class View is made of various nodes. To expand a node, you can click its + button. To collapse a node, you can click its - button. The top node displays the name of the project. Under the project node, the names of classes display. The bottom section of the Class View is used to display the members of a class. To see the members of a class, you can click it in the top window. Here is an example:

To add a new class, besides the main menu, you can right-click the name of the project in the Class View, position the mouse on Add, and click Class...

Declaring a Variable of a Class Type

Like any normal variable, to use a class in your program, you can first declare a variable for it. Like the variables we introduced in the previous lesson, to declare a variable of a class, type its name followed by a name for the variable. For example, to declare a variable of the above House class, you could type the following:

using System;

class House
{
}

class Program
{
    static void Main()
    {
        House property;
    }
}

A variable declared from a primitive type is called a value variable. This is because such a variable holds its value. The C# language supports another type of variable. This time, when you declare the variable, its name doesn't hold the value of the variable; it holds a reference to the address where the actual variable is stored in memory. This reference type is the kind used to declare a variable for a class.

To use a variable as reference, you must initialize it using an operator called new. Here is an example:

using System;

class House
{
}

class Program
{
    static void Main()
    {
        House property;
        
        property = new House();
    }
}

You can also use the new operator directly when declaring the variable as follows:

using System;

class House
{
}

class Program
{
    static void Main()
    {
        House property = new House();
    }
}

In C#, if you create a class in any of the files that belong to the same project, the class is made available to all other files of the same project.

Sharing Classes

Unlike its sisters the C and the C++ languages, C# was developed with the idea of working complementarily with other languages such as C++/CLI, Visual Basic, and J#. In other words, code from these other languages should be able to "read" or access code written in a C# application. To make this possible, a C# class can be created as a public object.

If you want your class to be accessible to code written in other languages, precede the class keyword with public when creating it. Here is an example:

using System;

public class Exercise
{
    static void Main()
    {
	int Number = 244;
	object Thing  = 1450.58;

	Console.WriteLine(Number);
	Console.WriteLine(Thing);
    }
}

Garbage Collection

When you initialize a variable using the new operator, you are in fact reserving some space in the heap memory. The memory is "allocated" for the variable. When that variable is no longer needed, such as when your program closes, it (the variable) must be removed from memory and the space it was using can be made available to other variables or other programs. This is referred to as garbage collection. In the past, namely in C/C++, this was a concern for programmers because they usually had to remember to manually delete such a variable (a pointer) and free its memory.

The .NET Framework solves the problem of garbage collection by "cleaning" the memory after you. This is done automatically when necessary so that the programmer doesn't need to worry about this issue.

 

Copyright © 2006-2007 FunctionX, Inc. Next