-2

I have Two class one is Employee another one is Program. In Program class it have Private method ViewEmployee() I need to call private method into another class??

  • 3
    Why `private` when you want to call it from outside the class? - but here you go: https://stackoverflow.com/questions/135443/how-do-i-use-reflection-to-invoke-a-private-method – Rand Random Jul 01 '21 at 06:11
  • private methods are not accessible outside the class . – Harkirat singh Jul 01 '21 at 06:18
  • The fact you want to call a private method leads me to suspect you have an XY problem....methods are private for a reason! – Mitch Wheat Jul 01 '21 at 06:19
  • This is all your own code? Why not just make that method public? Do you have an access modifier at all (private)? If not, just add it `public void ViewEmployee` – Hans Kesting Jul 01 '21 at 06:19

2 Answers2

2

You don't. The point of making it private is so your compiler can notify you that you cannot call it from another class.

If you want to call it from the same assembly, make it internal. If you want to call it from a different assembly, make it public.

nvoigt
  • 75,013
  • 26
  • 93
  • 142
1

Private methods are not accessible outside the class. See access modifiers in MSDN https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/access-modifiers

If you want to call it from another class in the same namespace then make it

internal

else if you want to call if from anywhere make it

public

.

Harkirat singh
  • 599
  • 6
  • 19