Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why and how does C# allow accessing private variables outside the class itself when it's within the same containing class?

Tags:

I don't know if the question is descriptive enough but why and how does this behaviour exist?:

public class Layer {     public string Name { get; set; }      private IEnumerable<Layer> children;     public IEnumerable<Layer> Children     {         get { return this.children.Where ( c => c.Name != null ).Select ( c => c ); }         set { this.children = value; }     }      public Layer ( )     {         this.children = new List<Layer> ( ); // Fine          Layer layer = new Layer ( );         layer.children = new List<Layer> ( ); // Isn't .children private from the outside?     } } 

I can access layer.Children anywhere, that's fine, but how can I access layer.children since it's private?

Layer layer = new Layer ( ); layer.children = new List<Layer> ( ); 

only works if the code is inside the Layer class. Is there special code to treat accessing private variables differently if it's done inside the containing class, even though the access is from the outside?

I know the reason of using:

this.children = ... 

inside the containing class, but creating new instances and modifying them from the outside, even if they are still within the containing class, doesn't seem like a good practice.

What's the reason for allowing this?

like image 459
Joan Venge Avatar asked Apr 20 '11 23:04

Joan Venge


People also ask

Why C-section is done?

A C-section might be recommended for women with certain health issues, such as a heart or brain condition. There's a blockage. A large fibroid blocking the birth canal, a pelvic fracture or a baby who has a condition that can cause the head to be unusually large (severe hydrocephalus) might be reasons for a C-section.

How is C-section done?

A C-section, also called a cesarean section or cesarean delivery, is a surgical procedure in which a baby is delivered through incisions in your abdomen and uterus. They're performed when a vaginal delivery is not possible or safe, or when the health of you or your baby is at risk.

Are C-sections painful?

You won't feel any pain during the C-section, although you may feel sensations like pulling and pressure. Most women are awake and simply numbed from the waist down using regional anesthesia (an epidural and/or a spinal block) during a C-section. That way, they are awake to see and hear their baby being born.

What means C-section?

Cesarean section, C-section, or Cesarean birth is the surgical delivery of a baby through a cut (incision) made in the mother's abdomen and uterus. Healthcare providers use it when they believe it's safer for the mother, the baby, or both.


Video Answer


1 Answers

See section 3.5.1 of the C# language specification. The relevant text is this:

Private, which is selected by including a private modifier in the member declaration. The intuitive meaning of private is “access limited to the containing type”.

Note that the modifier is relevant to the type, not the instance.

And then further in section 3.5.2 some rules are further explained:

In intuitive terms, when a type or member M is accessed, the following steps are evaluated to ensure that the access is permitted:

  • First, if M is declared within a type (as opposed to a compilation unit or a namespace), a compile-time error occurs if that type is not accessible.
  • Then, if M is public, the access is permitted.
  • Otherwise, if M is protected internal, the access is permitted if it occurs within the program in which M is declared, or if it occurs within a class derived from the class in which M is declared and takes place through the derived class type (§3.5.3).
  • Otherwise, if M is protected, the access is permitted if it occurs within the class in which M is declared, or if it occurs within a class derived from the class in which M is declared and takes place through the derived class type (§3.5.3).
  • Otherwise, if M is internal, the access is permitted if it occurs within the program in which M is declared.
  • Otherwise, if M is private, the access is permitted if it occurs within the type in which M is declared.
  • Otherwise, the type or member is inaccessible, and a compile-time error occurs.
like image 164
Anthony Pegram Avatar answered Dec 29 '22 13:12

Anthony Pegram