Class and Struct Accessibility

In this chapter you will learn:

  1. Accessibility in Class and Struct
  2. Class and Struct Member Accessibility
  3. What are the Access modifiers

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:

  1. What are the members in a class
Home » C# Tutorial » Class
Classes and Structs
Encapsulation
Class and Struct Accessibility
Members
Static Members
Class
Class instance variables
Class instance Methods
Class Inheritance
Class constructor
Default constructors
Parameters of constructors
Constructor overloading
Static Constructor
Destructor
Override destructor
new operator
Class field
Static Fields
Read only field
Field default value
const value
Abstract class
Static Class
Partial type
Methods
Method local variable
Method Overloading
Method overloading with ref and out
Type conversion and method overloading
Error in Method overloading
virtual methods
Override methods
Method Parameter
Pass parameters
ref parameters
out parameters
Variable parameter length
Optional parameters
Named parameters
static method
function Return
Recursive methods
Virtual methods and polymorphism
Cast
Shadow inherited members
Seal a member
this keyword
base keyword
base and name hiding
Access Specifiers
private
public
protected
internal
Indexer
String type indexer
Multidimensional indexer
Indexer overloading
Indexer to append
Indexer logic
Properties
Properties getter and setter
Accessibilities of properties
Automatic properties
Read only and write only property
Virtual properties
Abstract Properties
Static Properties
interface
interface implementation
Explicit interface implementation
Interface inheritance
Indexer in an interface
Interface Properties
struct and interface
virtual interface implementation
as operator
is operator
null reference
Nested classes
object class
ToString method
GetHashCode
Object initialization
Extension method
Struct Instances vs. Class Instances