IMPACT : CHANGING C# METHODS FROM PRIVATE TO PUBLIC

Mahesh KK

Total Post:4

Points:28
Posted by  Mahesh KK
C#
 1017  View(s)
Ratings:
Rate this:
Hello All,

i have a Windows form application based on C#, i would like to change few of its methods from Private to Public in order to use them in Matlab, I am quite new to handle this, please let me know how it will impact my application?
Is there any consequences? is it safe to do that?

Thanks in advance.
  1. Samuel Fernandes

    Post:159

    Points:1117
    Re: Impact : Changing C# Methods from Private to Public

    No, you can hide a public member with a private method in a subclass, but you cannot override a public member with a private one in a subclass. And, in actually, it's not just a public/private thing, this applies to narrowing the access in general.

     

    Revised: By hiding with a more restrictive access - in this case private access - you will still see the base class member from a base-class or sub-class reference, but it would defer to the new method when available from the new access level.

     

    So in general, when you hide, the hide takes precedence when visible at its access level. Otherwise the original method is the one used.

     

    public class BaseClass

    {

        public virtual void A() { }

     

        public virtual void B() { }

    }

     

    public class SubClass

    {

        // COMPILER ERROR, can't override to be more restrictive access

        private override void A() { }

     

        // LEGAL, you can HIDE the base class method, but which is chosen

        // depends on level accessed from

        private new void B() { }

    }

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!