Class creation

The general syntax to declare a class is


[public, 
 internal,
 abstract, 
 sealed, 
 static, 
 unsafe, 
 partial] class YourClassName [ Generic type parameters,  a base class, and interfaces]{
     methods, 
     properties, 
     indexers, 
     events, 
     fields, 
     constructors,
     operator functions, 
     nested types, 
     a finalizer
}

Class and Struct Accessibility

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:
public class Rectangle
{
    // protected method:
    protected void calculate() { }

    // private field:
    private int width = 3;

    // protected internal property:
    protected internal int Width
    {
        get { return width; }
    }
}

You can specify how accessible your types and their members are by using the access modifiers.

The access modifiers are

java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.