Cendo Cendo - 1 year ago 61
C# Question

Why I can't call all Methods from another class?

I try to call a method from another class. I've added the reference and find the class. I'm able to call some methods of this class to but I can't call all of the methods in this class.

The main class is

public partial class
and the class with the methods I want to use is
private static class
. The method I try to call is
public static
. And there is no difference between the methods I can call and the method I can't call. That's the problem I don't understand. Please can someone explain?

namespace MyNamespace2
private static class MyClass
public static void MyMethod() { }

namespace MyNamespace1
class Program
static void Main(string[] args)
//can't call

Answer Source

If the class you're trying to access is private it doesn't matter if its methods are public, you can't access the class itself.

Consider it this way. If you're not allowed to enter New York, it doesn't matter if all of the houses are open inside the city, since you can't get into the city itself.

To get back to code however there is a case in which you can access private classes: if the calling function is part of a class that contains your private class.

If you have the freedom to manipulate the code for the class you're trying to access then you should attempt something similar to this:

class A {
  private static class B {
    public static void Foo() {}

  public void Bar() {

In this code you can call B.Foo() from any member function of A, but not from outside of A.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download