Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
CSharpNotesForProfessionals.pdf
Скачиваний:
57
Добавлен:
20.05.2023
Размер:
6.12 Mб
Скачать

Chapter 40: Access Modifiers

Section 40.1: Access Modifiers Diagrams

Here are all access modifiers in venn diagrams, from more limiting to more accessible:

Access Modifier

Diagram

private

internal

protected

protected internal

GoalKicker.com – C# Notes for Professionals

162

public

Below you could find more information.

Section 40.2: public

The public keyword makes a class (including nested classes), property, method or field available to every consumer:

public class Foo()

{

public string SomeProperty { get; set; }

public class Baz

{

public int Value { get; set; }

}

}

public class Bar()

{

public Bar()

{

var myInstance = new Foo();

var someValue = foo.SomeProperty; var myNestedInstance = new Foo.Baz();

var otherValue = myNestedInstance.Value;

}

}

Section 40.3: private

The private keyword marks properties, methods, fields and nested classes for use inside the class only:

public class Foo()

{

private string someProperty { get; set; }

private class Baz

{

public string Value { get; set; }

}

public void Do()

{

var baz = new Baz { Value = 42 };

}

}

GoalKicker.com – C# Notes for Professionals

163

public class Bar()

{

public Bar()

{

var myInstance = new Foo();

//Compile Error - not accessible due to private modifier var someValue = foo.someProperty;

//Compile Error - not accessible due to private modifier var baz = new Foo.Baz();

}

}

Section 40.4: protected internal

The protected internal keyword marks field, methods, properties and nested classes for use inside the same assembly or derived classes in another assembly:

Assembly 1

public class Foo

{

public string MyPublicProperty { get; set; }

protected internal

string MyProtectedInternalProperty { get; set; }

protected internal

class MyProtectedInternalNestedClass

{

 

private string

blah;

public int N {

get; set; }

}

 

}

 

public class Bar

 

{

 

void MyMethod1()

 

{

 

Foo foo = new Foo();

var myPublicProperty = foo.MyPublicProperty;

var myProtectedInternalProperty = foo.MyProtectedInternalProperty; var myProtectedInternalNestedInstance =

new Foo.MyProtectedInternalNestedClass();

}

}

Assembly 2

public class Baz : Foo

{

void MyMethod1()

{

var myPublicProperty = MyPublicProperty;

var myProtectedInternalProperty = MyProtectedInternalProperty; var thing = new MyProtectedInternalNestedClass();

}

void MyMethod2()

GoalKicker.com – C# Notes for Professionals

164

{

Foo foo = new Foo();

var myPublicProperty = foo.MyPublicProperty;

// Compile Error

var myProtectedInternalProperty = foo.MyProtectedInternalProperty;

// Compile Error

var myProtectedInternalNestedInstance =

new Foo.MyProtectedInternalNestedClass();

}

}

public class Qux

{

void MyMethod1()

{

Baz baz = new Baz();

var myPublicProperty = baz.MyPublicProperty;

// Compile Error

var myProtectedInternalProperty = baz.MyProtectedInternalProperty;

// Compile Error

var myProtectedInternalNestedInstance =

new Baz.MyProtectedInternalNestedClass();

}

void MyMethod2()

{

Foo foo = new Foo();

var myPublicProperty = foo.MyPublicProperty;

//Compile Error

var myProtectedInternalProperty = foo.MyProtectedInternalProperty;

// Compile Error

var myProtectedInternalNestedInstance =

new Foo.MyProtectedInternalNestedClass();

}

}

Section 40.5: internal

The internal keyword makes a class (including nested classes), property, method or field available to every consumer in the same assembly:

internal class Foo

{

internal string SomeProperty {get; set;}

}

internal class Bar

{

var myInstance = new Foo();

internal string SomeField = foo.SomeProperty;

internal class Baz

{

private string blah; public int N { get; set; }

}

GoalKicker.com – C# Notes for Professionals

165