Class and Struct Accessibility
In this chapter you will learn:
Accessibility in Class and Struct
Classes and structs that are declared directly within a namespace can be either public or internal.
Internal is the default if no access modifier is specified.
Struct members, including nested classes and structs, can be declared as public, internal, or private.
Class members, including nested classes and structs, can be public, protected internal, protected, internal, or private.
The access level for class members and struct members, including nested classes and structs, is private by default.
Derived classes cannot have greater accessibility than their base types.
You can enable specific other assemblies to access your internal types by using the InternalsVisibleToAttribute.
Class and Struct Member Accessibility
Class members including nested classes and structs can be declared with any of the five types of access.
Struct members cannot be declared as protected because structs do not support inheritance.
User-defined operators must always be declared as public. Destructors cannot have accessibility modifiers.
The following code adds the Access Modifiers to the class
// public class:/* java 2 s. c om*/
public class Rectangle
{
// protected method:
protected void calculate() { }
// private field:
private int width = 3;
// protected internal property:
protected internal int Width
{
get { return width; }
}
}
Access modifiers
You can specify how accessible your types and their members are by using the access modifiers.
The access modifiers are
- public
- protected
- internal
- protected internal
- private.
Next chapter...
What you will learn in the next chapter: