Access Modifiers

Modifiers are C# keywords used to modify declarations of types and type members.
types - classes, structs, interfaces, enum
type members - field, properties, methods, indexers

ModifierTypeDescription
abstractClasses
Methods
Indicates that a class is just a base class.
Indicates that a method must have a new implementation in a derived class
In VB.Net this is MustInherit | MustOverride
constConstantsIndicates these values are known at compile time and do not change
externMethodsIndicates that a method is implemented externally
internalClasses
Methods
Properties
Indicates the member can be accessed by any code in the same assembly but not from other assemblies.
This is the default for classes and structures.
A member of an internal class that has a public modifier is actually internal (check)
In VB.Net this is Friend.
In VB.Net this is the default for classes, structures, interfaces, delegates, enumerations and modules.
new Replaces the member in the base class.
When a member of a derived class has the same name as its base class then the derived class instance replaces that in the base class.
This is the default for any members with the same name but using this modifier will remove the automatic warning.
In VB.Net this is Shadows
overrideMethodsIndicates that the method is a new implementation for a base class virtual member.
In VB.Net this is Overrides
partialClasses
Methods
Allows you to split a class definition into separate source files.
publicClasses
Methods
Properties
Constants
Indicates the member can be accessed by any code in the same assembly or another assembly that references it. There are no restrictions.
It is completely visible.
Can be called from outside that class and from other assemblies.
This is the default for namespaces, interfaces and enumerations.
In VB.Net this is the default for methods.
privateClasses
Methods
Properties
Constants
Indicates the member can only be accessed by code inside the class or struct.
It is good practice to keep all fields private.
This is the default for (nested) classes and structures.
protectedMethodsIndicates the member can be accessed only by code inside the class or structure OR in a class derived from that class.
protected internal Indicates the method can be accessed by any code in the same assembly OR from within a derived class in another assembly.
Using 'internal protected' will also work but is never used.
readonlyConstantsCan only be assigned a value in the declaration or in the constructor
sealedClassesIndicates that a class cannot be inherited.
Indicates that a method cannot have a new implementation in a derived class (cannot be overridden)
In VB.Net this is NotInheritable | NotOverridable
staticConstants
Fields
Methods
Indicates that a member belongs to the type itself and does not require an instance of the object
A class can provide a container for methods that do not have to modify any internal instance fields or properties.
A static class is the same as having only static members and a private constructor
A static class is also a sealed class.
In VB.Net this is Shared
unsafeMethods
Properties
Required for any operation involving pointers
virtualMethods
Properties
A method may be overridden.
A class can be overridden in a derived class.
A method can be modified by the operating system, the hardware or a concurrently executing thread.
In VB.Net this is Overrideable
volatileFieldsCan be modified by something else, eg hardware or a concurrent thread

© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited TopPrevNext