T-DUP
T-DUP

Reputation: 41

C# Access Modifiers : restrict some methods to only one other class

Kind of a noobie question but I'm curious and I can't find an answer to my question:

I have:

public class Profile
{
public void methodA(){}
}

public class User
{
private List<Profile> listProfiles;
public void test() { App.Handler.doA(listProfiles[0]);} //let's say we have 1 profile
}

public class Handler
{
public void doA(Profile) { profile.methodA();}
}

I want to restrict the use of methodA() to only my Handler Class, is it possible via a key word or some other way?

The purpose is to have everything going through the Handler.

Thanks :)

Upvotes: 1

Views: 1257

Answers (2)

Regfor
Regfor

Reputation: 8091

Not possible using standard means. You can use workarounds:

  • nest classes in one and then use private keys.
  • use separate assembly for such classes and restrict usage by internal keyword

Upvotes: 0

leppie
leppie

Reputation: 117220

You will have to nest Handler inside the Profile class and the you can make methodA private.

Upvotes: 1

Related Questions