Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Override methods not included in overload search????

Override methods not included in overload search????

Scheduled Pinned Locked Moved C#
csharphtmlcomquestion
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    David Wengier
    wrote on last edited by
    #1

    Why!?! I found this interesting bit of text in the C# specification at MSDN here[^], when trying to find out why my code isnt working: First, the set of all accessible members named N declared in T and the base types of T is constructed. Declarations that include an override modifier are excluded from the set. Can anyone explain to me why? It seems illogical. Consider the following:

    class A {
    public virtual int Foo () { return 1; }
    }
    class B : A {
    new int Foo () { return 2; }
    static void Main () { System.Console.WriteLine (new C ().Foo ()); }
    }
    class C : B {
    public override int Foo () { return 3; }

    It outputs 2, because the code is executed in B. If B didn't define a Foo() method, it would output 1. Logically (well, to me anyway) it should output 3! -- Dave

    M 1 Reply Last reply
    0
    • D David Wengier

      Why!?! I found this interesting bit of text in the C# specification at MSDN here[^], when trying to find out why my code isnt working: First, the set of all accessible members named N declared in T and the base types of T is constructed. Declarations that include an override modifier are excluded from the set. Can anyone explain to me why? It seems illogical. Consider the following:

      class A {
      public virtual int Foo () { return 1; }
      }
      class B : A {
      new int Foo () { return 2; }
      static void Main () { System.Console.WriteLine (new C ().Foo ()); }
      }
      class C : B {
      public override int Foo () { return 3; }

      It outputs 2, because the code is executed in B. If B didn't define a Foo() method, it would output 1. Logically (well, to me anyway) it should output 3! -- Dave

      M Offline
      M Offline
      manivannan p
      wrote on last edited by
      #2

      Hi David. w.r.to, the code sample,,,, class B : A { new int Foo () { return 2; } static void Main () {System.Console.WriteLine (new C ().Foo ()); } } This is an apt example for Hiding Methods. The statement 'new int Foo () { return 2; }' in Class B , informs the compiler that the base class member has been hidden and hence it continues executing the method in B. w.r.to the statement, If B didn't define a Foo() method, it would output 1....check out it would output only '3'. I hope is this what you wanted to clarify. regards, Manivannan.P

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups