locked
Accessing the private method through an instance in a static method RRS feed

  • Question

  • Hi,

    I have class with a private method and a static method. In static method I created an instance of the class and tried to call the private method on the instance.

    The application is working without any issues. 

    My question is , how is it possible to call a private method on the instance. Please correct my understanding.

     class Program
        {
            
            static void Main(string[] args)
            {
                Console.WriteLine(testClass.GetDataStatic());
                Console.ReadLine();
            }

        }


        class testClass
        {
            string _test = "test";
           
           
            private string GetData()
            {
                return _test;
            }

            public static string GetDataStatic()
            {
                return (new testClass()).GetData();
            }
        }

    Tuesday, August 30, 2016 7:47 AM

Answers

  • There are four access modifiers in C# which are as follows

    public
    protected
    internal
    private

    There are 5 accessibility levels using these access modifiers

    public: Access is not restricted and can call in other class and assembly.
    protected: Access is limited to the containing class or types derived from the containing class.
    Internal: Access is limited to the current assembly.
    protected internal: Access is limited to the current assembly or types derived from the containing class.
    private: Access is limited to the containing type.

    The Private access is the least permissive access level. Private members are accessible only within the body of the class or the struct in which they are declared.

    In this example, the testClass class contains private method GetData(). As private members, they cannot be accessed except by member methods. Public methods named GetDataStatic() is allow controlled access to the private members. 

    Here I would like few points which are
    1. The Non Static members always call by class instance/object in static method as GetData() is non static method calls in GetStaticData() method.
    2. The static method can call non static method by class instance only.
    As you have two methods GetData() which is non static and another is GetDataStatic() which is static method so a static method always have static data but when you need to call non static data and method in static method then need to create instance of class to call these members. If both are static and in same class then no need to create instance of class and call directly by name. for example if GetData() is also a static method then no need to create instance of class. 

    So it's not necessary to call private method by instance but when in static method we call another non-static member then we need to create instance of class.

    Conclusion: Non static member calls in static method by class instance/object even these are in same class and doesn't depend on access modifier.  If GetData() is public method then it also call by class instance in static method GetDataStatic() even both are in same class. So here Static is playing role rather then access modifier in your example.


    • Edited by Sandeep Shekhawat Tuesday, August 30, 2016 9:04 AM update
    • Proposed as answer by CoolDadTx Wednesday, August 31, 2016 5:40 PM
    • Marked as answer by Kevin Linq Wednesday, September 7, 2016 1:59 AM
    Tuesday, August 30, 2016 8:56 AM

All replies

  • There are four access modifiers in C# which are as follows

    public
    protected
    internal
    private

    There are 5 accessibility levels using these access modifiers

    public: Access is not restricted and can call in other class and assembly.
    protected: Access is limited to the containing class or types derived from the containing class.
    Internal: Access is limited to the current assembly.
    protected internal: Access is limited to the current assembly or types derived from the containing class.
    private: Access is limited to the containing type.

    The Private access is the least permissive access level. Private members are accessible only within the body of the class or the struct in which they are declared.

    In this example, the testClass class contains private method GetData(). As private members, they cannot be accessed except by member methods. Public methods named GetDataStatic() is allow controlled access to the private members. 

    Here I would like few points which are
    1. The Non Static members always call by class instance/object in static method as GetData() is non static method calls in GetStaticData() method.
    2. The static method can call non static method by class instance only.
    As you have two methods GetData() which is non static and another is GetDataStatic() which is static method so a static method always have static data but when you need to call non static data and method in static method then need to create instance of class to call these members. If both are static and in same class then no need to create instance of class and call directly by name. for example if GetData() is also a static method then no need to create instance of class. 

    So it's not necessary to call private method by instance but when in static method we call another non-static member then we need to create instance of class.

    Conclusion: Non static member calls in static method by class instance/object even these are in same class and doesn't depend on access modifier.  If GetData() is public method then it also call by class instance in static method GetDataStatic() even both are in same class. So here Static is playing role rather then access modifier in your example.


    • Edited by Sandeep Shekhawat Tuesday, August 30, 2016 9:04 AM update
    • Proposed as answer by CoolDadTx Wednesday, August 31, 2016 5:40 PM
    • Marked as answer by Kevin Linq Wednesday, September 7, 2016 1:59 AM
    Tuesday, August 30, 2016 8:56 AM
  • Every public method of a class can call any private or protected method of a class and access any field and property of said class.
    With properties we often make private set functiosn for the sole purpose of calling them in class code like the constructor, but not other public code.

    However having a public static method create a instance and then call a private instance function is incredibly wierd. I can not think how that would make any sense. Why not simply make GetData() static in itself?

    If you were return the testClass instance after some initialsiation, it would be a Factory Pattern.
    But just instantiating it to run a instance function, return the instance functions result and let the instance run out of scope? That seems very odd. I would even say counter-intuitive and possibly a really bad idea to solve a problem. You should propably tell us of the problem you tried to solve, there might be a easier way or your approach might be very wrong.


    Remember to mark helpfull answers as helpfull and close threads by marking answers.

    Tuesday, August 30, 2016 9:26 AM
  •  Hi aneesha1:

    Thanks for posting there .

    First ,I tested your code ,it does ran successfully .

    Secondly, the keyword is the access modifier in your static method .

    When I changed public static string GetDataStatic(); to  private  static string GetDataStatic();

    The VS send an error message "CS0122  C# is inaccessible due to its protection level"

    It told us we could not call a private static method from one class to another class .

    By the way ,we can call any type of method in the instance . 

    I hope my reply would do help to you .

    If you think it is helpful , please mark it .

    Best regards .

    Kevin   


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place. Click HERE to participate the survey.

    Wednesday, August 31, 2016 10:48 AM