ZengCode.Com (The Thai Php Framework)  


Home   Download   Manual   About us    

Facebook   


MAIN MENU
เขียนโปรแกรมบน iPhone ด้วย MonoTouch
News
Php Tips
Ubuntu
Spring+Strut+Hibernate
Android Programming
Design Pattern By PHP
C# Design Pattern
Linux Quick Tips
C# Tips & Technique
C# using Linq น่าใช้จริงๆ
Java & JavaScript Tips
MAVEN
Database & SQL
ZengCode Framework Guide
Mac OSx
Zeng Code Code
Programming
IPhone (Tips and Trick)

Download เอกสารที่น่าสนใจ

     How to override methods in C# How to override methods in C#   (2009-09-04)

How to override methods in C# 

 
 
 
 

 
Introduction

When an instance method declaration includes an override modifier, the method is said to be an override method.

What is...

An override method overrides an inherited virtual method with the same signature. Whereas a virtual method declaration introduces a new method, an override method declaration specializes an existing inherited virtual method by providing a new implementation of the method.

How you can get into trouble...

It is an error for an override method declaration to include any of the following modifiers: new, static, or virtual. An override method declaration may include the abstract modifier. This enables a virtual method to be overridden by an abstract method.

More detail...
The method overridden by an override declaration is known as the overridden base method. For an override method M declared in a class C, the overridden base method is determined by examining each base class of C, starting with the direct base class of C and continuing with each successive direct base class, until an accessible method with the same signature as M is located.

For example...

For the purposes of locating the overridden base method, a method is considered accessible if it is public, or if it is internal and declared in the same program as C.

A compile-time error occurs unless all of the following are true for an override declaration:

• An overridden base method can be located as described above.
• The overridden base method is a virtual, abstract, or override method.


In other words, the overridden base method cannot be static or non-virtual.

• The overridden base method is not a sealed method.
• The override declaration and the overridden base method have the same declared accessibility.


In other words, an override declaration cannot change the accessibility of the virtual method. In the example

class A
{
    int x;
    public virtual void PrintFields() {
        Console.WriteLine("x = {0}", x);
    }
}


class B: A
{
    int y;
    public override void PrintFields() {
        base.PrintFields();
        Console.WriteLine("y = {0}", y);
    }
}

the base.PrintFields() invocation in B invokes the PrintFields method declared in A. A base-access disables the virtual invocation mechanism and simply treats the base method as a non-virtual method. Had the invocation in B been written ((A)this).PrintFields(), it would recursively invoke the PrintFields method declared in B, not the one declared in A, since PrintFields is virtual and the run-time type of ((A)this) is B.

Only by including an override modifier can a method override another method. In all other cases, a method with the same signature as an inherited method simply hides the inherited method. In the example

class A
{
    public virtual void F() {}
}
class B: A
{
    public virtual void F() {}        // This will change the state of the F()
}


the F method in B doesn't have an override modifier and therefore does'n override the F method in A. Rather, the F method in B hides the method in A, and a warning is reported because the declaration does not include a new modifier.
In the example

class A
{
    public virtual void F() {}
}
class B: A
{
    new private void F() {}            // A.F is now hidden and can not be retrived from within B
}
class C: B
{
    public override void F() {}    // Now you can retrive A.F
}



the F method in B hides the virtual F method inherited from A. Because the new F in B has private access, it only includes the class body of B and does not extend to C. The declaration of F in C is therefore permitted to override the F inherited from A.

For more info:
Inside C# (preview edition
C# support

 


Comment
Name
Comment
Security CodeCAPTCHA Image

web hit counter

This page took 0.043474 seconds to load.