Skip to Main Content
C# in a Nutshell, Second Edition
book

C# in a Nutshell, Second Edition

by Peter Drayton, Ben Albahari, Ted Neward
August 2003
Intermediate to advanced content levelIntermediate to advanced
928 pages
32h 1m
English
O'Reilly Media, Inc.
Content preview from C# in a Nutshell, Second Edition

Access Modifiers

To promote encapsulation, a type or type member may hide itself from other types or other assemblies by adding one of the following five access modifiers to the declaration:

public

The type or type member is fully accessible. This is the implicit accessibility for enum members (see Section 3.6 later in this chapter) and interface members (see Section 3.5 later in this chapter).

internal

The type or type member in assembly A is accessible only from within A. This is the default accessibility for nonnested types, and so may be omitted.

private

The type member in type T is accessible only from within T. This is the default accessibility for class and struct members, and so may be omitted.

protected

The type member in class C is accessible only from within C, or from within a class that derives from C.

protected internal

The type member in class C and assembly A is accessible only from within C, from within a class that derives from C, or from within A. Note that C# has no concept of protected and internal, whereby “a type member in class C and assembly A is accessible only from within C, or from within a class that both derives from C and is within A.”

Note that a type member may be a nested type. Here is an example of using access modifiers:

// Assembly1.dll using System; public class A { private int x=5; public void Foo( ) {Console.WriteLine (x);} protected static void Goo( ) { } protected internal class NestedType { } } internal class B { private void Hoo ( ) { A a1 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

C# in a Nutshell

C# in a Nutshell

Ben Albahari, Ted Neward, Peter Drayton
C# 7.0 in a Nutshell

C# 7.0 in a Nutshell

Joseph Albahari, Ben Albahari
C# Cookbook, 2nd Edition

C# Cookbook, 2nd Edition

Jay Hilyard, Stephen Teilhet
C# Cookbook

C# Cookbook

Joe Mayo

Publisher Resources

ISBN: 0596005261Catalog PageErrata