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. overloading and overriding

overloading and overriding

Scheduled Pinned Locked Moved C#
questioncsharp
3 Posts 3 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.
  • L Offline
    L Offline
    lakshmi_sri
    wrote on last edited by
    #1

    what is the most important factor to consider when overloading and overriding in c#?

    P G 2 Replies Last reply
    0
    • L lakshmi_sri

      what is the most important factor to consider when overloading and overriding in c#?

      P Offline
      P Offline
      Phillip M Hoff
      wrote on last edited by
      #2

      When overloading methods, one thing to keep in mind is consistency of the API. For example, suppose we have a method DoSomething() that ultimately requires an integer parameter and, optionally, string and boolean parameters. One way we could do this is the following: void DoSomething(int required1); void DoSomething(string option1, int required1); void DoSomething(bool option2, string option1, int required1); While this technically works, it's not a consistent API. How often are users going to have to consult their documentation to remember the order in which parameters are expected? Even worse, imagine that your optional parameters are of the same type. How easy would it be then to accidentally swap arguments passed to the method? Yes, Intellisense helps, but it's not a substitute for a simple API and, besides, not everyone uses Visual Studio. Consistency further applies to the default values of those optional parameters. In general, "missing" parameters should have the same default value in each overloaded method. When overriding methods, an important thing to keep in mind is maintaining the semantics of the base class method. That is, when a base class marks a method as virtual it expects that method to behave within certain constraints, despite the fact that the derived class can theoretically do whatever it likes within that method. For example, in a file stream hierarchy it might be that the ReadByte() method throw an exception if called before the Open() method. For a Windows Forms control it might be that the overridden OnClicked() method in the derived class must call the base class method (in order for the Clicked event to be fired). For the developer of the API, maintaining semantics means documenting, in detail, the expectations of derived classes with respect to overriding methods. For the user of the API, it means ensuring derived classes meet those expectations. -Phil

      1 Reply Last reply
      0
      • L lakshmi_sri

        what is the most important factor to consider when overloading and overriding in c#?

        G Offline
        G Offline
        Guffa
        wrote on last edited by
        #3

        Stuck on your test? ;)

        --- b { font-weight: normal; }

        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