A Closer Look at C# Classes - Part 1


© Jose Aniceto

C# supports object-oriented programming and like any object-oriented language, defining classes and objects is the foundation to everything. If you've been following the articles on this site, by now you would know that in C#, a class is defined using the class keyword. In C#, everything is contained in a class. A function declaration cannot exist on its own without being part of a class. That is why, the program entry point in C#, which is Main(), has to be a member of a class.

The following is a general expression of how a class is defined in C#. Anything enclosed in the square brackets is optional.

[attribute] [access-modifier] class identifier [:base-class] class-body [;]

Tip


Unlike C++, C# does not require a semicolon at the end of a class definition.

There are times when you will want to define a class but leave it to other programmers to implement the details. This is in particularly good in an environment where multiple developers are working together. In C#, you can define such a class as an abstract class.

An abstract class cannot be instantiated. That is if you try to create an object from an abstract class the compiler will complain. You would have to inherit the class in order for you to instantiate it. Listing 1 shows you how to use an abstract class.

Listing 1
abstract class Mammal {
void Walk() {
}
}

sealed class Human : Mammal {
void Walk() {
System.Console.WriteLine("Humans walk on two legs.\n");
}
}

// -- will not work
// class Humanoid : Human {
// void Walk() {
// System.Console.Writeline("Humaniods.\n"):
// }
// }

public class Example {
static void Main() {
class Human Athlete = new Human();
// class Mammal Athlete = new Mammal(); -- will not work
Athlete.Walk();
}
}

If you are required to instantiate a class from an abstract class, a sealed class on the other hand does not allow you to instantiate a class. This prevents accidental inheritance where abuse and misuse of a class is prevented. The string class library, which is part of the .Net platform, is an example of a sealed class. The string class cannot be inherited.

In Listing 1, you can see that the class Human is a sealed class. The Humanoid class, which tries to inherit from Human, will not work and the compiler will not allow it because Human is a sealed class. Notice also in the Main() function, that a object is being instantiated from the abstract class definition.

Go To Page: 1 2


The copyright of the article A Closer Look at C# Classes - Part 1 in C# Programming is owned by . Permission to republish A Closer Look at C# Classes - Part 1 in print or online must be granted by the author in writing.

Post this Article to facebook Add this Article to del.icio.us! Digg this Article furl this Article Add this Article to Reddit Add this Article to Technorati Add this Article to Newsvine Add this Article to Windows Live Add this Article to Yahoo Add this Article to StumbleUpon Add this Article to BlinkLists Add this Article to Spurl Add this Article to Google Add this Article to Ask Add this Article to Squidoo