Class Definitions in C


Class Definitions in C#

C# uses the class keyword to define classes. The basic structure required is:

 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, you can specify that the class is public and should also be accessible to code in other projects. To do this, you use the public keyword:

 public class MyClass {    // Class members. }
Note

Note that classes declared in their own right in this way cannot be private or protected. However, it is possible to use these modifiers for declaring classes as class members, which you look at in the next chapter.

In addition to these two access modifier keywords, you can also specify that the class is either abstract (cannot be instantiated, only inherited, and can have abstract members) or sealed (cannot be inherited). To do this, you use one of the two mutually exclusive keywords abstract or sealed. An abstract class must, therefore, be declared in the following way:

 public abstract class MyClass {    // Class members, may be abstract. }

Here MyClass is a public abstract class, while internal abstract classes are also possible.

Sealed classes are declared as follows:

 public sealed class MyClass {    // Class members. }

As with abstract classes, sealed classes may be public or internal.

Inheritance can also be specified in the class definition. To do this, you simply put a colon after the class name, followed by the base class name. For example:

 public class MyClass : MyBase {    // Class members. } 

Note that only one base class is permitted in C# class definitions and that if you inherit from an abstract class you must implement all the abstract members inherited (unless the derived class is also abstract).

The compiler will not allow a derived class to be more accessible than its base class. This means that an internal class can inherit from a public base, but a public class can't inherit from an internal base. This means that the following code is legal:

 public class MyBase {    // Class members. }     internal class MyClass : MyBase {    // Class members. }

But the following code won't compile:

 internal class MyBase {    // Class members. }     public class MyClass : MyBase {    // Class members. }

If no base class is used, then the class will inherit only from the base class System.Object (which has the alias object in C#). Ultimately, all classes have System.Object at the root of their inheritance hierarchy. You take a closer look at this fundamental class a little later.

As well as specifying base classes in this way, you can also specify interfaces supported after the colon character. If a base class is specified, it must be the first thing after the colon, with interfaces specified afterward. If there is no base class specified, you specify the interfaces straight after the colon. Commas must be used to separate the base class name (if there is one) and the interface names from one another.

For example, you could add an interface to MyClass as follows:

 public class MyClass : IMyInterface {    // Class members. }

All interface members must be implemented in any class that supports the interface, although you can provide an "empty" implementation (with no functional code) if you don't want to do anything with a given interface member, and you can implement interface members as abstract in abstract classes.

The following declaration is invalid, because the base class MyBase isn't the first entry in the inheritance list:

 public class MyClass : IMyInterface, MyBase {    // Class members. }

The correct way to specify a base class and an interface is as follows:

 public class MyClass : MyBase, IMyInterface {    // Class members. }

And remember that multiple interfaces are possible, so the following is also valid:

 public class MyClass : MyBase, IMyInterface, IMySecondInterface {    // Class members. }

As a quick recap, the following table shows the allowed access modifier combinations for class definitions.

Modifier

Meaning

none or internal

Class accessible only from within the current project

public

Class accessible from anywhere

abstract or internal abstract

Class accessible only from within the current project, cannot be instantiated, only derived from

public abstract

Class accessible from anywhere, cannot be instantiated, only derived from

sealed or internal sealed

Class accessible only from within the current project, cannot be derived from, only instantiated

public sealed

Class accessible from anywhere, cannot be derived from, only instantiated

Interface Definitions

Interfaces are declared in a similar way to classes, but using the interface keyword rather than class. For example:

 interface IMyInterface { // Interface members. } 

The access modifier keywords public and internal are used in the same way, so to make an interface publicly accessible you must use the public keyword:

 public interface IMyInterface {    // Interface members. }

The keywords abstract and sealed are not allowed in interfaces because neither modifier makes sense in the context of interfaces (they contain no implementation, so they can't be instantiated directly, and they must be inheritable to be useful).

Interface inheritance is also specified in a similar way to class inheritance. The main difference here is that multiple base interfaces can be used, for example:

 public interface IMyInterface : IMyBaseInterface, IMyBaseInterface2 {    // Interface members. }

Interfaces are not classes, and thus do not inherit from System.Object. However, the members of System.Object are available via an interface type variable, purely for convenience.

Also, as already discussed, it is impossible to instantiate an interface in the same way as a class. The following Try It Out provides an example of some class definitions, along with some code that uses them.

Try It Out – Defining Classes

image from book
  1. Create a new console application called Ch09Ex01 in the directory C:\BegVCSharp\Chapter9.

  2. Modify the code in Program.cs as follows:

    namespace Ch09Ex01 { public abstract class MyBase { } internal class MyClass : MyBase { } public interface IMyBaseInterface { } internal interface IMyBaseInterface2 { } internal interface IMyInterface : IMyBaseInterface, IMyBaseInterface2 { } internal sealed class MyComplexClass : MyClass, IMyInterface { }        class Program    {       static void Main(string[] args)       { MyComplexClass myObj = new MyComplexClass(); Console.WriteLine(myObj.ToString()); Console.ReadKey();       }    } }

  3. Execute the project. The output is shown in Figure 9-1.

    image from book
    Figure 9-1

How It Works

This project defines classes and interfaces in the inheritance hierarchy shown in Figure 9-2.

image from book
Figure 9-2

I've included Program here because it is a class defined in the same way as the other classes, even though it isn't part of the main class hierarchy. the Main() method possessed by this class is the entry point for your application as discussed earlier in the book.

MyBase and IMyBaseInterface are public definitions, so they are available from other projects. The other classes and interfaces are internal and are only available in this project.

The code in Main() calls the ToString() method of myObj, an instance of MyComplexClass:

MyComplexClass myObj = new MyComplexClass(); Console.WriteLine(myObj.ToString());

This is one of the methods inherited from System.Object (not shown in the diagram because I've omitted the members of this class for clarity) and simply returns the class name of the object as a string, qualified by any relevant namespaces.

image from book




Beginning Visual C# 2005
Beginning Visual C#supAND#174;/sup 2005
ISBN: B000N7ETVG
EAN: N/A
Year: 2005
Pages: 278

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net