public class Rectangle : Shape {
public override sealed double Area() {
return this.length * this.width;
}
}
The only way to add a new method Diagonal()
to the Rectangle
class is to create a new class that derives from it, like this:
public class NewRectangle : Rectangle {
public double Diagonal() {
return Math.Sqrt(Math.Pow(this.length, 2) + Math.Pow(this.width, 2));
}
}
In C# 3.0, you just use the new extension method feature to add a new method to an existing type. To add the Diagonal()
method to the existing Rectangle
class, define a new static class and define the extension method (a static method) within it, like this:
public static class MethodsExtensions {
public static double Diagonal(this Rectangle rect) {
return Math.Sqrt(Math.Pow(rect.length, 2) + Math.Pow(rect.width, 2));
}
}
In this example, Diagonal()
is the extension method that is added to the Rectangle
class. You can use the Diagonal()
method just like a method from the Rectangle
class:
Rectangle r = new Rectangle();
r.length = 4;
r.width = 5;
//---prints out: 6.40312423743285---
Console.WriteLine(r.Diagonal());
The first parameter of an extension method is prefixed by the this
keyword, followed by the type it is extending (Rectangle in this example, indicating to the compiler that this extension method must be added to the Rectangle
class). The rest of the parameter list (if any) is then the signature of the extension method. For example, to pass additional parameters into the Diagonal()
extension method, you can declare it as:
public static double Diagonal(this Rectangle rect, int x, int y) {
//---additional implementation here---
return Math.Sqrt(Math.Pow(rect.length, 2) + Math.Pow(rect.width, 2));
}
To call this modified extension method, simply pass in two arguments, like this:
Console.WriteLine(r.Diagonal(3,4));
Figure 6-4 shows IntelliSense providing a hint on the parameter list.
Figure 6-4
Although an extension method is a useful new feature in the C# language, use it sparingly. If an extension method has the same signature as another method in the class it is trying to extend, the method in the class will take precedence and the extension method will be ignored.
Chapter 4 discussed two primary access modifiers — public
and private
, and introduced two others: protected
and internal
. Let's take a look at how the latter are used. Consider the following class definition:
public class A {
private int v;
public int w;
protected int x;
internal int y;
protected internal int z;
}
The A
class has four data members, each with a different access modifiers. The fifth data member, z
, has a combination of two access modifiers — protected
and internal
. To see the difference between all these different modifiers, create an instance of A
and observe the members displayed by IntelliSense.
Figure 6-5 shows that only the variables w
, y
, and z
are accessible.
Figure 6-5
At this moment, you can conclude that:
□ The private
keyword indicates that the member is not visible outside the type (class).
□ The public
keyword indicates that the member is visible outside the type (class).
□ The protected
keyword indicates that the member is not visible outside the type (class).
□ The internal
keyword indicates that the member is visible outside the type (class).
□ The protected internal
keyword combination indicates that the member is visible outside the type (class).
Now define a second class, B
, that inherits from class A
:
public class B : A {
public void Method() {}
}
Try to access the class A
variables from within Method()
. In Figure 6-6, IntelliSense shows the variables that are accessible.
Figure 6-6
As you can see, member x
is now visible (in addition to w
, y
, and z
), so you can conclude that:
□ The private
keyword indicates that the member is not visible outside the type (class) or to any derived classes.
□ The public
keyword indicates that the member is visible outside the type (class) and to all derived classes.
□ The protected
keyword indicates that the member is not visible outside the type (class) but is visible to any derived classes.
□ The internal
keyword indicates that the member is visible outside the type (class) as well as to all derived classes.
□ The protected internal
keyword combination indicates that the item is visible outside the type (class) as well as to all derived classes.
From these conclusions, the difference among private
, public
, and protected
is obvious. However, there is no conclusive difference between internal
and protected internal
. The internal
access modifier indicates that the member is only visible within its containing assembly. The protected internal
keyword combination indicates that the member is visible to any code within its containing assembly as well as derived types.
Besides applying the access modifiers to data members, you can also use them on type definitions. However, you can only use the private
and public
access modifiers on class definitions.
Inheritance and Constructors
Consider the following BaseClass
definition consisting of one default constructor:
public class BaseClass {
//---default constructor---
public BaseClass() {
Console.WriteLine("Constructor in BaseClass");
}
}
Anther class, DerivedClass
inheriting from the BaseClass
, also has a default constructor:
public class DerivedClass : BaseClass {
//---default constructor---
public DerivedClass() {
Console.WriteLine("Constructor in DerivedClass");
}
}
So when an object of DerivedClass
is instantiated, which constructor will be invoked first? The following statement shows that the constructor in the base class will be invoked before the constructor in the current class will be invoked:
DerivedClass dc = new DerivedClass();
Читать дальше