Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9221505
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T03:35:38+00:00 2026-06-18T03:35:38+00:00

If the method iAmPrivate is Private then how does the following execute it? If

  • 0

If the method iAmPrivate is Private then how does the following execute it? If I try to run PrivateMethod’s myPrivateMethod it is encapsulated why doesn’t the same apply to iAmPrivate ?

class PrivateMethod {
    private int myPrivateMethod() {
        return 1;
    }
}

static void Main(string[] args) {

    Program myProgram = new Program();

    myProgram.iAmPrivate("private");
    myProgram.iAmPublic("public");

    PrivateMethod pm = new PrivateMethod();
    //Console.WriteLine("this won't run {0}", pm.myPrivateMethod);  //not possible          

    Console.WriteLine("press [enter] to exit");
    Console.ReadLine();
}

public void iAmPublic(string s) {
    Console.WriteLine("I am {0}", s);
}

private void iAmPrivate(string s) {
    Console.WriteLine("I am {0}", s);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T03:35:39+00:00Added an answer on June 18, 2026 at 3:35 am

    [Thanks for the help… Now I get it!]

    Changing the class PrivateMethod to the following helped me understand what is going on:

    class PrivateMethod {
        public int pretendMain() {
            PrivateMethod x = new PrivateMethod();
            return x.myPrivateMethod();
        }
        private int myPrivateMethod() {
            return 1;
        }
    }
    
    class Program {
        static void Main(string[] args) {
    
            Program myProgram = new Program();
    
            myProgram.iAmPrivate("private");
            myProgram.iAmPublic("public");
    
            PrivateMethod pm = new PrivateMethod();
            Console.WriteLine("this will now run {0}", pm.pretendMain()); //now possible !
            //Console.WriteLine("this won't run {0}", pm.myPrivateMethod);  //not possible          
    
            Console.WriteLine("press [enter] to exit");
            Console.ReadLine();
        }
    
        public void iAmPublic(string s) {
            Console.WriteLine("I am {0}", s);
        }
        private void iAmPrivate(string s) {
            Console.WriteLine("I am {0}", s);
        }
    }
    

    The fact that I was missing is that myProgram.iAmPrivate("private"); was being run from within Main so of course it is accessible.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Method to see jQuery files are loaded completely then execute the function related to
Method sp.setVvalue(app.spvalue.intValue()); doesn't scroll vbox to desired position. mvPane = new VBox(); mvPane.setStyle(-fx-background-color: #fff;);
Method 'Private Sub trigger_Triggered(sender As Object, e As OamsIDD.VoamsWorldIDD.OamsStateChangeEventArgs)' cannot handle event 'Public Event
what method/algorithm does iis 7 use to generate etags. I'm looking for exact, not
Method return should be like if entered a number, suppose 345, then output should
Method visibility in Ruby (public, protected, and private methods) has been well explained in
My method returns an array of answers by mysqli_fetch_assoc. I try to extract the
Using method chaining I would like to amend the following code so that on
A method I am calling in run() in a class that implements Runnable )
Method overloading allows us to define many methods with the same name but with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.