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 Rectangleclass 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 Rectangleclass, 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 Rectangleclass. You can use the Diagonal()method just like a method from the Rectangleclass:
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 thiskeyword, followed by the type it is extending (Rectangle in this example, indicating to the compiler that this extension method must be added to the Rectangleclass). 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 — publicand private, and introduced two others: protectedand 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 Aclass has four data members, each with a different access modifiers. The fifth data member, z, has a combination of two access modifiers — protectedand internal. To see the difference between all these different modifiers, create an instance of Aand observe the members displayed by IntelliSense.
Figure 6-5 shows that only the variables w, y, and zare accessible.
Figure 6-5
At this moment, you can conclude that:
□ The privatekeyword indicates that the member is not visible outside the type (class).
□ The publickeyword indicates that the member is visible outside the type (class).
□ The protectedkeyword indicates that the member is not visible outside the type (class).
□ The internalkeyword indicates that the member is visible outside the type (class).
□ The protected internalkeyword 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 Avariables from within Method(). In Figure 6-6, IntelliSense shows the variables that are accessible.
Figure 6-6
As you can see, member xis now visible (in addition to w, y, and z), so you can conclude that:
□ The privatekeyword indicates that the member is not visible outside the type (class) or to any derived classes.
□ The publickeyword indicates that the member is visible outside the type (class) and to all derived classes.
□ The protectedkeyword indicates that the member is not visible outside the type (class) but is visible to any derived classes.
□ The internalkeyword indicates that the member is visible outside the type (class) as well as to all derived classes.
□ The protected internalkeyword 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 protectedis obvious. However, there is no conclusive difference between internaland protected internal. The internalaccess modifier indicates that the member is only visible within its containing assembly. The protected internalkeyword 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 privateand publicaccess modifiers on class definitions.
Inheritance and Constructors
Consider the following BaseClassdefinition consisting of one default constructor:
public class BaseClass {
//---default constructor---
public BaseClass() {
Console.WriteLine("Constructor in BaseClass");
}
}
Anther class, DerivedClassinheriting 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 DerivedClassis 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();
Читать дальше