none
Access modifier inside method in C# RRS feed

  • Question

  • Hello,

    Why we can't access modifier inside C# method?

     class Class1
        {
    
            public void M()
            {
                private int a = 10; //Error
            }
        }

    Monday, March 19, 2018 8:27 AM

Answers

  • From MSDN Docs:

    Access modifiers are keywords used to specify the declared accessibility of a member or a type.

    In the above case M() is a member of the type Class1 so that's why you are able to add public keyword to it but the int a  is not so you can't.

    The variable declared inside a method is only accessible within that method scope and cannot be accessed outside of it so, you don't need to set it's accessibility using keyword. So you just need to use it without access modifiers:

    public void M()
    {
       int a = 10; // now is fine
    }

    Hope it helps.


    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered"Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]


    Blog | LinkedIn | Stack Overflow | Facebook
    profile for Ehsan Sajjad on Stack Exchange, a network of free, community-driven Q&A sites






    • Edited by Ehsan Sajjad Monday, March 19, 2018 1:10 PM
    • Proposed as answer by Ehsan Sajjad Monday, March 19, 2018 1:10 PM
    • Marked as answer by Arash_89 Monday, March 19, 2018 2:41 PM
    Monday, March 19, 2018 12:49 PM
  • As an addition to Ehsan's response there is no logic on adding them since when you declare let's say a variable inside a method, that variable lives and dies inside that method. Only that method can access a variable that is created and defined in it, and when the method executes and stops then the variable you declared stops to exist, it gets deleted from the memory.
    • Marked as answer by Arash_89 Monday, March 19, 2018 2:41 PM
    Monday, March 19, 2018 1:44 PM
  • Hi Arash,

    What everyone is saying is that access modifiers are not needed inside method, and to avoid confusion the language doesn't allow it. Inside a method your members are always private.


    My Technet Articles

    If you like this or another reply, vote it up!
    If you think this or another reply answers the original question, mark it or propose it as an answer.


    Mauricio Feijo
    www.mauriciofeijo.com


    • Edited by Mauricio Feijo Monday, March 19, 2018 2:32 PM
    • Marked as answer by Arash_89 Monday, March 19, 2018 2:42 PM
    Monday, March 19, 2018 2:31 PM

All replies

  • From MSDN Docs:

    Access modifiers are keywords used to specify the declared accessibility of a member or a type.

    In the above case M() is a member of the type Class1 so that's why you are able to add public keyword to it but the int a  is not so you can't.

    The variable declared inside a method is only accessible within that method scope and cannot be accessed outside of it so, you don't need to set it's accessibility using keyword. So you just need to use it without access modifiers:

    public void M()
    {
       int a = 10; // now is fine
    }

    Hope it helps.


    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered"Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]


    Blog | LinkedIn | Stack Overflow | Facebook
    profile for Ehsan Sajjad on Stack Exchange, a network of free, community-driven Q&A sites






    • Edited by Ehsan Sajjad Monday, March 19, 2018 1:10 PM
    • Proposed as answer by Ehsan Sajjad Monday, March 19, 2018 1:10 PM
    • Marked as answer by Arash_89 Monday, March 19, 2018 2:41 PM
    Monday, March 19, 2018 12:49 PM
  • As an addition to Ehsan's response there is no logic on adding them since when you declare let's say a variable inside a method, that variable lives and dies inside that method. Only that method can access a variable that is created and defined in it, and when the method executes and stops then the variable you declared stops to exist, it gets deleted from the memory.
    • Marked as answer by Arash_89 Monday, March 19, 2018 2:41 PM
    Monday, March 19, 2018 1:44 PM
  • Hi Arash,

    What everyone is saying is that access modifiers are not needed inside method, and to avoid confusion the language doesn't allow it. Inside a method your members are always private.


    My Technet Articles

    If you like this or another reply, vote it up!
    If you think this or another reply answers the original question, mark it or propose it as an answer.


    Mauricio Feijo
    www.mauriciofeijo.com


    • Edited by Mauricio Feijo Monday, March 19, 2018 2:32 PM
    • Marked as answer by Arash_89 Monday, March 19, 2018 2:42 PM
    Monday, March 19, 2018 2:31 PM