Click here to Skip to main content
15,887,746 members
Home / Discussions / C#
   

C#

 
AnswerRe: Google Maps Pin
Paladin200010-Jun-10 4:41
Paladin200010-Jun-10 4:41 
GeneralRe: Google Maps Pin
JohnUSA10-Jun-10 4:53
JohnUSA10-Jun-10 4:53 
GeneralRe: Google Maps Pin
Paladin200010-Jun-10 5:20
Paladin200010-Jun-10 5:20 
AnswerRe: Google Maps Pin
Adam R Harris10-Jun-10 4:52
Adam R Harris10-Jun-10 4:52 
QuestionTricky Inheritance Question Pin
Jon Myers10-Jun-10 2:47
Jon Myers10-Jun-10 2:47 
AnswerRe: Tricky Inheritance Question Pin
Andrew Rissing10-Jun-10 4:21
Andrew Rissing10-Jun-10 4:21 
GeneralRe: Tricky Inheritance Question Pin
Jon Myers10-Jun-10 5:40
Jon Myers10-Jun-10 5:40 
GeneralRe: Tricky Inheritance Question Pin
Andrew Rissing10-Jun-10 7:34
Andrew Rissing10-Jun-10 7:34 
It sounds like your close to the correct understanding. Be aware though, this is NOT a normal situation in coding. The only reason you would ever use new is that the member is NOT virtual and you need to override its functionality. Because the method is virtual, there is really no need to shadow it with new. Typically shadowing can get you into a lot of confusion if you're not careful, hence why when its not necessary it is strictly avoided.

As an example of why its not needed, if you change the definition of the method in C to:

C#
class C : B
{
    public override void M() { Console.WriteLine("C"); }
}

It'll print out four "D"s.

As for interfaces, they don't represent a class you can create (eg. new IList()), but rather just an expected contract of functionality.

I mention explicit implementation of interfaces because they represent something similar to the behavior seen in the code you posted. Basically, how you reference an object determines exactly what functionality is called.

When you down cast class D to C, that is an implicit cast (ie. .NET assumes you mean to do that). It'd be exactly the same as if you had typed:

C#
C c = (C)d;


Ok, he comes some really confusing stuff, hopefully it won't make things worse.

If you look at the reflected IL code of the Main method, you'll see this:

.method private hidebysig static void Main(string[] args) cil managed
{
    .entrypoint
    .maxstack 1
    .locals init (
        [0] class Example.D d,
        [1] class Example.C c,
        [2] class Example.B b,
        [3] class Example.A a)
    L_0000: nop 
    L_0001: newobj instance void Example.D::.ctor()
    L_0006: stloc.0 
    L_0007: ldloc.0 
    L_0008: stloc.1 
    L_0009: ldloc.1 
    L_000a: stloc.2 
    L_000b: ldloc.2 
    L_000c: stloc.3 
    L_000d: ldloc.0 
    L_000e: callvirt instance void Example.C::M()
    L_0013: nop 
    L_0014: ldloc.1 
    L_0015: callvirt instance void Example.C::M()
    L_001a: nop 
    L_001b: ldloc.2 
    L_001c: callvirt instance void Example.A::M()
    L_0021: nop 
    L_0022: ldloc.3 
    L_0023: callvirt instance void Example.A::M()
    L_0028: nop 
    L_0029: ret 
}


I've underlined the calls to d.M(), c.M(), etc. You'll notice that its going back to the class that defines the original signature of the method. Because of the 'new' keyword, that inheritance starts over so to speak. So, from a code perspective, it'll burrow its way through the currently defined type you're referencing it as, until it finds a 'new' keyword or the 'virtual' keyword. From there, it'll find the furthest up the inheritance chain it can go before it reaches the real type of the class or another 'new' keyword. By using 'new', you break all inheritance hence forth.

I hope that helps you, rather than confuses you.
QuestionAuto serial software Pin
ostad_mrn10-Jun-10 2:39
ostad_mrn10-Jun-10 2:39 
AnswerRe: Auto serial software Pin
#realJSOP10-Jun-10 2:49
mve#realJSOP10-Jun-10 2:49 
QuestionRe: Auto serial software Pin
ostad_mrn10-Jun-10 3:13
ostad_mrn10-Jun-10 3:13 
AnswerRe: Auto serial software Pin
#realJSOP10-Jun-10 4:26
mve#realJSOP10-Jun-10 4:26 
QuestionHow to find duplicate values in two or more Excel files? Pin
bebef_198710-Jun-10 2:27
bebef_198710-Jun-10 2:27 
AnswerRe: How to find duplicate values in two or more Excel files? Pin
Adam R Harris10-Jun-10 4:58
Adam R Harris10-Jun-10 4:58 
QuestionCan I pass binary data of a file to WebBrowser control? Pin
Ahmad Safwat10-Jun-10 1:32
Ahmad Safwat10-Jun-10 1:32 
AnswerRe: Can I pass binary data of a file to WebBrowser control? Pin
Luc Pattyn10-Jun-10 1:51
sitebuilderLuc Pattyn10-Jun-10 1:51 
GeneralRe: Can I pass binary data of a file to WebBrowser control? Pin
Ahmad Safwat10-Jun-10 1:56
Ahmad Safwat10-Jun-10 1:56 
AnswerRe: Can I pass binary data of a file to WebBrowser control? Pin
Giorgi Dalakishvili10-Jun-10 2:14
mentorGiorgi Dalakishvili10-Jun-10 2:14 
GeneralRe: Can I pass binary data of a file to WebBrowser control? Pin
Ahmad Safwat10-Jun-10 2:45
Ahmad Safwat10-Jun-10 2:45 
GeneralRe: Can I pass binary data of a file to WebBrowser control? Pin
Luc Pattyn10-Jun-10 3:44
sitebuilderLuc Pattyn10-Jun-10 3:44 
GeneralRe: Can I pass binary data of a file to WebBrowser control? Pin
Ahmad Safwat10-Jun-10 4:14
Ahmad Safwat10-Jun-10 4:14 
GeneralRe: Can I pass binary data of a file to WebBrowser control? Pin
Luc Pattyn10-Jun-10 4:28
sitebuilderLuc Pattyn10-Jun-10 4:28 
GeneralRe: Can I pass binary data of a file to WebBrowser control? [modified] Pin
Hristo-Bojilov10-Jun-10 6:23
Hristo-Bojilov10-Jun-10 6:23 
GeneralRe: Can I pass binary data of a file to WebBrowser control? Pin
Luc Pattyn10-Jun-10 7:02
sitebuilderLuc Pattyn10-Jun-10 7:02 
GeneralRe: Can I pass binary data of a file to WebBrowser control? Pin
Hristo-Bojilov10-Jun-10 7:30
Hristo-Bojilov10-Jun-10 7:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.