O'Reilly logo

Beginning Microsoft® Visual C#® 2008 by Eric White, Morgan Skinner, Jon D. Reid, Jacob Hammer Pedersen, Christian Nagel, Karli Watson

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 9. Defining Classes

In Chapter 8, you looked at the features of object-oriented programming (OOP). In this chapter, you put theory into practice and define classes in C#. You won't go so far as to define class members in this chapter but will concentrate on the class definitions themselves. That may sound a little limiting, but don't worry—there's plenty here to get your teeth into!

To start, you explore the basic class definition syntax, the keywords you can use to determine class accessibility and more, and the way in which you can specify inheritance. You also look at interface definitions, because they are similar to class definitions in many ways.

The rest of the chapter covers various related topics that apply when defining classes in C#, including the following:

  • The System.Object class.

  • Helpful tools provided by VS and VCE.

  • Class libraries.

  • A comparison between interfaces and abstract classes.

  • Struct types.

  • Copying objects.

Class Definitions in C#

C# uses the class keyword to define classes:

class MyClass
{
   // Class members.
}

This code defines a class called MyClass. Once you have defined a class, you are free to instantiate it anywhere else in your project that has access to the definition. By default, classes are declared as internal, meaning that only code in the current project will have access to them. You can specify this explicitly using the internal access modifier keyword as follows (although you don't have to):

internal class MyClass
{
   // Class members.
}

Alternatively, ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required