C# Friend classes and OOP Composition

Solution 1:

Strictly speaking, you can't define a specific class (or list of classes) that you can expose the data to. You can, however, use the internal access modifier instead of private, which makes the members available to any class in the same assembly.

That being said, you should strongly consider exposing these members through properties rather than fields (which is what I'm guessing you're planning on exposing). Doing this will allow the class to define exactly how that information can be exposed to other classes and what--if anything--should happen when another class changes the data.

Solution 2:

May be this might help you..

public class A
{
    public A() { }

    public string AccessData(object accessor)
    {
        if (accessor is B)
            return "private_data";
        else
            throw new UnauthorizedAccessException();
    }
}

public class B
{
    public B() { }

    private void AccessDataFromA()
    {
        Console.WriteLine(new A().AccessData(this));
    }
}

Solution 3:

You could define both in an assembly and mark the fields in A as "internal".

A more OO approach is to provide a set of public methods in A that allow access to the data (or a subset of the data), typically via simple properties.