none
What is the use of sealed methods in C#? RRS feed

  • Question

  • I understand that the sealed modifier is used with overridden methods in derived class to prevent further override. But what i don't understand is we can also create a non virtual methods which cannot be overridden by derived classes. Why can't we use such non virtual methods instead of sealed classes? Is it useful when we have multilevel inheritance? How exactly sealed methods used in c#?
    Tuesday, May 21, 2019 5:29 AM

All replies

  • A non-virtual method can certainly be overridden in derived classes, by using the "new" keyword.  "sealed" prevents that.

    In my opinion, sealing a method is not a good idea.  No matter how smart you are, someone down the road is going to think of a more clever way to do what you've done, and by using a sealed method you've made that harder.


    Tim Roberts | Driver MVP Emeritus | Providenza & Boekelheide, Inc.

    Tuesday, May 21, 2019 7:09 AM
  • Hi Newuserms123,

    Thank you for posting here.

    For your question, you want to know the use of sealed methods in c#.

    I think the following link could help you to understand it.

    https://www.c-sharpcorner.com/UploadFile/puranindia/what-are-sealed-classes-and-sealed-methods/​​​​​​​

    Note:This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. Microsoft does not control these sites and has not tested any software or information found on these sites; Therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, May 21, 2019 7:18 AM
    Moderator
  • My understanding is Sealed keyword is always used with override and it cannot be used with new. In c#   new is used to hide the base class implementation and it's not exactly overriding.
    Tuesday, May 21, 2019 8:35 AM
  • Hi

    Thanks for the feedback.

    >>My understanding is Sealed keyword is always used with override and it cannot be used with new. In c#   new is used to hide the base class implementation and it's not exactly overriding.

    Yes, you are right. I want to know what your current question is.

    Best Regards,

    Jack

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, May 22, 2019 1:19 AM
    Moderator