views:

514

answers:

5

Consider a verbatim copy of Bloch's Builder pattern (with changes made for C#'s syntax):

public class NutritionFacts
{
  public int ServingSize { get; private set; }
  public int Servings { get; private set; }
  public int Calories { get; private set; }
  ...
  public class Builder
  {
    private int ServingSize { get; set; }
    private int Servings { get; set; }
    private int Calories { get; set; }

    public Builder(int servingSize, int servings)
    {
      ServingSize = servingSize;
      Servings = servings;
    }

    public Builder Calories(int calories)
    { Calories = calories; return this; }

    public NutritionFacts Build()
    {
      return new NutritionFacts(this);
    }
  }

  private NuitritionFacts(Builder builder)
  {
    ServingSize = builder.ServingSize;
    Servings = builder.Servings;
    Calories = builder.Calories;
  }
}

If you try to run this, the C# compiler will complain that it doesn't have permission to access the private properties of Builder. However, in Java, you can do this. What rule is different in C# that prevents you from accessing private properties of nested classes?

(I realize that people have given alternatives here and that's great. What I'm interested is why you can't use the Java pattern without modification).

+10  A: 

In Java private members of inner/nested classes are accessible to the containing class. In C# they aren't.

Laurence Gonsalves
+2  A: 

I don't see why that should be allowed to compile. You are trying to access the private fields of a class from outside that class. Java, however, contains a special rule for nested classes that allows access from the outer class.

JoshJordan
+1  A: 

The accessibility levels in C# are as follows:

  • public: Access is not restricted.
  • protected: Access is limited to the containing class or types derived from the containing class.
  • internal: Access is limited to the current assembly.
  • protected internal: Access is limited to the current assembly or types derived from the containing class.
  • private: Access is limited to the containing type.

There is no special case in C# for nested classes, as a result you cannot access a private member from outside that class or any class deriving from that class.

You can find more information in the follow MSDN article: Accessibility Levels (C#)

Richard C. McGuire
My understanding is there is a kind of special case for C# nested classes. They can access the privates of enclosing types.
Tom Hawtin - tackline
+1  A: 

Hi I asked this exact question.Got some pretty good answers. Here we go

http://stackoverflow.com/questions/512651/how-is-javas-notion-of-static-different-from-cs

uriDium
A: 

Gilad Bracha argues that allowing outer classes access to the privates of nested classes breaks "the subsumption rule of type systems for OO languages."

Tom Hawtin - tackline