Is there a way to restrict access to a public method to only a specific class in C#?

后端 未结 5 1426
萌比男神i
萌比男神i 2021-01-12 06:30

I have a class A with a public method in C#. I want to allow access to this method to only class B. Is this possible?

UPDATE:

This is what i

5条回答
  •  伪装坚强ぢ
    2021-01-12 06:52

    You can if you make class A a private, nested class inside class B:

    class B
    {
        class A
        {
            public Int32 Foo { get; set; }
        }
    }
    

    Only B will be able to see A and it's members in this example.

    Alternatively you could nest B inside A:

    class A
    {
        Int32 Foo { get; set; }
    
        public class B { }
    }
    

    In this case everyone can see both A and B but only B can see A.Foo.

提交回复
热议问题