□ One specific to the IConsoleLogging interface that can be accessed only via an instance of the IConsoleLogging interface.

You cannot use the public access modifier on the explicit interface methods' implementation.

To invoke these implementations of the LogError() method, use the following statements:

//---create an instance of Calculation---

Calculation c = new Calculation();

//---prints out: Some error message here---

c.LogError('Some error message here');

//---create an instance of IFileLogging---

IFileLogging f = c;

//---prints out: In IFileLogging: Some error message here---

f.LogError('Some error message here');

//---create an instance of IConsoleLogging---

IConsoleLogging l = c;

//---prints out: In IConsoleLogging: Some error message here---

l.LogError('Some error message here');

Another use of explicit interface member implementation occurs when two interfaces have the same method name but different signatures. For example:

public interface IFileLogging {

 void LogError(string str);

}

public interface IConsoleLogging {

 void LogError();

}

Here, the LogError() method in the IFileLogging interface has a string input parameter, while there is no parameter in the IConsoleLogging interface. When you now implement the two interfaces, you can provide two overloaded LogError() methods, together with an implementation specific to each interface as illustrated here:

public class Calculation : IFileLogging, IConsoleLogging {

 //---common to both interfaces---

 public void LogError(string str) {

  Console.WriteLine('In LogError(str): ' + str);

 }

 public void LogError() {

  Console.WriteLine('In LogError()');

 }

 //---only available to the IFileLogging interface---

 void IFileLogging.LogError(string str) {

  Console.WriteLine('In IFileLogging: ' + str);

 }

 //---only available to the IConsoleLogging interface---

 void IConsoleLogging.LogError() {

  Console.WriteLine('In IConsoleLogging');

 }

}

As you can see , the first two LogError() methods are overloaded and are common to both interfaces. This means that you can access them via an instance of the Calculation class. The next two implementations are specific to the IFileLogging and IConsoleLogging interfaces and can be accessed only via an instance of each interface:

//---create an instance of Calculation---

Calculation c = new Calculation();

//---prints out: In LogError()---

c.LogError();

//---prints out: In LogError(str)---

c.LogError('Some error message here');

//---create an instance of IFileLogging---

IFileLogging f = c;

//---prints out: In IFileLogging: Some error message here--- 

f.LogError('Some error message here');

//---create an instance of IConsoleLogging---

IConsoleLogging l = c;

//---prints out: In IConsoleLogging--- 

l.LogError();

Abstract Classes versus Interfaces

An abstract class defines the members and optionally provides the implementations of each member. Members that are not implemented in the abstract class must be implemented by classes that inherit from it.

An interface, on the other hand, defines the signatures of members but does not provide any implementation. All the implementations must be provided by classes that implement it.

So which one should you use? There are no hard-and-fast rules, but here are a couple of points to note:

□ You can add additional members to classes as and when needed. In contrast, once an interface is defined (and implemented by classes), adding additional members will break existing code.

□ Classes support only single-inheritance but can implement multiple interfaces. So if you need to define multiple contracts (rules) for a type, it is always better to use an interface.

Summary

This chapter explained how inheritance works in C# and the types of inheritances available — implementation and interface. One important topic covered in this chapter is that of abstract class versus interface, both of which have their uses in C#.

The chapter also described how you can provide overloaded methods and operators, as well as add capabilities to a class without deriving from it by using the extension method feature new in C# 3.0.

Chapter 7

Delegates and Events

Two of the most important aspects of object-oriented programming are delegates and events. A delegate

Вы читаете C# 2008 Programmer's Reference
Добавить отзыв
ВСЕ ОТЗЫВЫ О КНИГЕ В ИЗБРАННОЕ

0

Вы можете отметить интересные вам фрагменты текста, которые будут доступны по уникальной ссылке в адресной строке браузера.

Отметить Добавить цитату