Introduction

Pre-requisites
Roadmap

- Diving in OOP (Day 1): Polymorphism and Inheritance(Early Binding/Compile Time Polymorphism)
- Diving in OOP (Day 2): Polymorphism and Inheritance (Inheritance)
- Diving in OOP (Day 3): Polymorphism and Inheritance (Dynamic Binding/Run Time Polymorphism)
- Diving in OOP (Day 4): Polymorphism and Inheritance (All about Abstarct classes in C#)
- Diving in OOP (Day 5): All about access modifiers in C# (Public/Private/Protected/Internal/Sealed/Constants/Readonly Fields)
- Diving in OOP (Day 6): Understanding Enum in C# (A Practical Approach)
- Diving into OOP (Day 7): Properties in C# (A Practical Approach)
- Diving into OOP (Day 8): Indexers in C# (A Practical Approach)
- Diving into OOP (Day 9): Understanding Events in C# (An Insight)
Access Modifiers
“Access modifiers (or access specifiers) are keywords in object-oriented languages that set the accessibility of classes, methods, and other members. Access modifiers are a specific part of programming language syntax used to facilitate the encapsulation of components.”
Public, Private, Protected at Class Level
private
for class members.private
for class members.AccessModifiers
. You’ll get a Program.cs class file by default. In the same file, add a new class named Modifiers
and add the following code to it:using System;
namespace AccessModifiers
{
class Modifiers
{
static void AAA()
{
Console.WriteLine(“Modifiers AAA”);
}
public static void BBB()
{
Console.WriteLine(“Modifiers BBB”);
AAA();
}
}
class Program
{
static void Main(string[] args)
{
Modifiers.BBB();
}
}
}
Modifiers
and two static
methods AAA
and BBB
. Method BBB
is marked as public
. We call the method BBB
from Main
method.The method is called directly by the class name because it is marked static
.Output
Modifiers BBB Modifiers AAA
BBB
is marked public
and so anyone is allowed to call and run it. Method AAA
is not marked with any access modifier which automatically makes it private
, that is the default. The private
modifier has no effect on members of the same class and so method BBB
is allowed to call method AAA
. Now this concept is called member access.Program
class and try to access AAA
as:class Program { static void Main(string[] args) { Modifiers.AAA(); Console.ReadKey(); } }
Output
'AccessModifiers.Modifiers.AAA()' is inaccessible due to its protection level
AAA
is private
, therefore no one else can have access to it except Modifiers
class.AAA
method as protected, our class looks like:class Modifiers { protected static void AAA() { Console.WriteLine("Modifiers AAA"); }
public static void BBB()
{
Console.WriteLine(“Modifiers BBB”);
AAA();
}
}
Program
class Program { static void Main(string[] args) { Modifiers.AAA(); Console.ReadKey(); } }
Output
'AccessModifiers.Modifiers.AAA()' is inaccessible due to its protection level
AAA
even after we introduced a new modifier namedprotected
. But BBB
can access AAA
method because it lies in the same class.Modifiers in Inheritance
Modifiers Base Class
class ModifiersBase { static void AAA() { Console.WriteLine("ModifiersBase AAA"); } public static void BBB() { Console.WriteLine("ModifiersBase BBB"); } protected static void CCC() { Console.WriteLine("ModifiersBase CCC"); } }
Modifiers Derive Class
class ModifiersDerived:ModifiersBase { public static void XXX() { AAA(); BBB(); CCC(); } }
Program Class
class Program { static void Main(string[] args) { ModifiersDerived.XXX(); Console.ReadKey(); } }
Output
'AccessModifiers.ModifiersBase.AAA()' is inaccessible due to its protection level
protected
, we are actually telling C# that only derived classes can access that method and no one else can. Therefore in method XXX, we can call CCC because it is marked protected
, but it cannot be called from anywhere else including Main
function. The method AAA
is made private
and can be called only from the class ModifiersBase
. If we remove AAA
from method XXX
, the compiler will give no error.Private
means only the same class has access to the members, public
means everybody has access and protected
lies in between where only derived classes have access to the base class method.Internal Modifier at Class Level
AccessModifiersLibrary
” in your Visual Studio. Add a class named ClassA
in that class library and mark the class as internal
, the code will be as shown below:AccessModifiersLibrary.ClassA:
namespace AccessModifiersLibrary
{
internal class ClassA
{
}
}
AccessModifiers
” i.e. created earlier. Add the reference ofAccessModifiersLibrary
library by adding its compiled DLL as a reference to AccessModifiers
.AccessModifiers
console application, modify the Program
class like shown below:AccessModifiers.Program
using AccessModifiersLibrary;
namespace AccessModifiers
{
class Program
{
static void Main(string[] args)
{
ClassA classA;
}
}
}
Output
Compile time error: 'AccessModifiersLibrary.ClassA' is inaccessible due to its protection level
internal
means that we can only access ClassA
fromAccessModifiersLibrary.dll and not from any other file or code. Internal
modifier means that access is limited to current program only. So try never to create a component and mark the class internal
as no one would be able to use it.internal
from ClassA
, will the code compile? i.e.,AccessModifiersLibrary.ClassA
namespace AccessModifiersLibrary { class ClassA { } }
AccessModifiers.Program
using AccessModifiersLibrary;
namespace AccessModifiers
{
class Program
{
static void Main(string[] args)
{
ClassA classA;
}
}
}
Output
Compile time error: 'AccessModifiersLibrary.ClassA' is inaccessible due to its protection level
internal
. So our class ClassA
is internal
by default even if we do not mark it with any access modifier, so the compiler results remain the same.ClassA
been marked public
, everything would have gone smooth without any error.internal
can only be have its access limited to the current assembly only.Namespaces with Modifiers
AccessModifiers
class library as public
in Program
class:Program
public namespace AccessModifiers { class Program { static void Main(string[] args) {
}
}
}
Output
Compile time error: A namespace declaration cannot have modifiers or attributes
public
and we cannot add any other access modifier including public
again too.Private Class
Program
as private
, so our code becomes:namespace AccessModifiers { private class Program { static void Main(string[] args) {
}
}
}
Output
Compile time error: Elements defined in a namespace cannot be explicitly declared as private, protected, or protected internal
public
or internal
. It cannot be marked as protected
or private
. The default is internal
for the class.private
.private
.namespace AccessModifiers { public class Program { static void Main(string[] args) { }
public private void Method1()
{
}
}
}
Output
Compile time error: More than one protection modifier
int
and object
have no accessibility restrictions. They can be used anywhere and everywhere.Internal Class and Public Method
ClassA
marked internal
and have a public
method MethodClassA()
, as:namespace AccessModifiersLibrary { internal class ClassA { public void MethodClassA(){} } }
MethodClassA
of ClassA
.Program
using AccessModifiersLibrary;
namespace AccessModifiers
{
public class Program
{
public static void Main(string[] args)
{
ClassA classA = new ClassA();
classA.MethodClassA();
}
}
}
Output
'AccessModifiersLibrary.ClassA' is inaccessible due to its protection level The type 'AccessModifiersLibrary.ClassA' has no constructors defined 'AccessModifiersLibrary.ClassA' is inaccessible due to its protection level 'AccessModifiersLibrary.ClassA' does not contain a definition for 'MethodClassA' and no extension method 'MethodClassA' accepting a first argument of type 'AccessModifiersLibrary.ClassA' could be found (are you missing a using directive or an assembly reference?)
MethodClassA
of ClassA
is public
, it could not be accessed in Program
class due to protection level of ClassA
, i.e. internal
. The type enclosing the method MethodClassA
is internal
, so no matter if the method is marked public
, we cannot access it in any other assembly.Public Class and Private Method
ClassA
as public
and method as private
:AccessModifiersLibrary.ClassA:
namespace AccessModifiersLibrary
{
public class ClassA
{
private void MethodClassA(){}
}
}
Program
using AccessModifiersLibrary;
namespace AccessModifiers
{
public class Program
{
public static void Main(string[] args)
{
ClassA classA = new ClassA();
classA.MethodClassA();
}
}
}
Output on compilation
'AccessModifiersLibrary.ClassA' does not contain a definition for 'MethodClassA' and no extension method 'MethodClassA' accepting a first argument of type 'AccessModifiersLibrary.ClassA' could be found (are you missing a using directive or an assembly reference?)
Public
, still can’t access the private
method. So for accessing a member of the class, the access modifier of class as well as method is very important.Public Class and Internal Method
ClassA
as public
and MethodClassA
as internal
:AccessModifiersLibrary.ClassA:
namespace AccessModifiersLibrary
{
public class ClassA
{
Internal void MethodClassA(){}
}
}
Program
using AccessModifiersLibrary;
namespace AccessModifiers
{
public class Program
{
public static void Main(string[] args)
{
ClassA classA = new ClassA();
classA.MethodClassA();
}
}
}
Output on compilation
'AccessModifiersLibrary.ClassA' does not contain a definition for 'MethodClassA' and no extension method 'MethodClassA' accepting a first argument of type 'AccessModifiersLibrary.ClassA' could be found (are you missing a using directive or an assembly reference?)
internal
marked member means that no one from outside that DLL can access the member.Protected Internal
ClassA
, ClassB
and ClassC
, and place the code somewhat like this:namespace AccessModifiersLibrary { public class ClassA { protected internal void MethodClassA() {
}
}
public class ClassB:ClassA
{
protected internal void MethodClassB()
{
MethodClassA();
}
}
public class ClassC
{
public void MethodClassC()
{
ClassA classA=new ClassA();
classA.MethodClassA();
}
}
}
Program
class in our console application, call the MethodClassC
of ClassC
.Program
using AccessModifiersLibrary;
namespace AccessModifiers
{
public class Program
{
public static void Main(string[] args)
{
ClassC classC=new ClassC();
classC.MethodClassC();
}
}
}
Compiler output
The code successfully compiles with no error.
Protected internal
modifier indicates two things, that either the derived class or the class in the same file can have access to that method, therefore in the above mentioned scenario, the derived class ClassB
and the class in the same file, i.e., ClassC
can access that method of ClassA
marked as protected internal
.Protected internal
means that the derived class and the class within the same source code file can have access.Protected Member
namespace AccessModifiers { class AAA { protected int a; void MethodAAA(AAA aaa,BBB bbb) { aaa.a = 100; bbb.a = 200; } } class BBB:AAA { void MethodBBB(AAA aaa, BBB bbb) { aaa.a = 100; bbb.a = 200; } } public class Program { public static void Main(string[] args) { } } }
Compiler Output
Cannot access protected member 'AccessModifiers.AAA.a' via a qualifier of type 'AccessModifiers.AAA'; the qualifier must be of type 'AccessModifiers.BBB' (or derived from it)
AAA
is containing a protected
member, i.e., a
. But to the same class, no modifiers make sense. However as a
isprotected
, in the derived class method MethodBBB
, we cannot access it through AAA
as aaa.a
gives us an error. However bbb
which looks like BBB
does not give an error. To check this out, comment out the line aaa.a=100
inMethodBBB ()
. This means that we cannot access the protected
members from an object of the base class, but from the objects of derived class only. This is in spite of the fact that a
is a member of AAA
i.e. the base class. Even so, we still cannot access it. Also we cannot access a
from the method Main
.Accessibility Priority in Inheritance
Program
namespace AccessModifiers { class AAA {
}
public class BBB:AAA
{
}
public class Program
{
public static void Main(string[] args)
{
}
}
}
Compiler Output
Compile time error: Inconsistent accessibility: base class 'AccessModifiers.AAA' is less accessible than class 'AccessModifiers.BBB'
public
and internal
, public
always allows greater access to its members.AAA
is by default marked internal
and BBB
that derives from AAA
is made public
explicitly. We got an error as the derived class BBB
has to have an access modifier which allows greater access than the base class access modifier. Here internal
seems to be more restrictive than public
.ClassA
marked as public
and ClassB internal
or default, we get rid of the error.Program
namespace AccessModifiers { class AAA {
}
public class BBB
{
public AAA MethodB()
{
AAA aaa= new AAA();
return aaa;
}
}
public class Program
{
public static void Main(string[] args)
{
}
}
}
Compiler output
Inconsistent accessibility: return type 'AccessModifiers.AAA' is less accessible than method 'AccessModifiers.BBB.MethodB()'
AAA
is internal
which is more restrictive than public
. The accessibility of method MethodB
is public
which is more than that of the typeAAA
. Now the error occurred because return values of a method must have greater accessibility than that of the method itself, which is not true in this case.Program
namespace AccessModifiers { class AAA {
}
public class BBB
{
public AAA aaa;
}
public class Program
{
public static void Main(string[] args)
{
}
}
}
Compiler Output
Inconsistent accessibility: field type 'AccessModifiers.AAA' is less accessible than field 'AccessModifiers.BBB.aaa'

AAA
or data type aaa
is internal
. aaa
field is public
which makes it more accessible than AAA
which is internal
. So we got the error.namespace AccessModifiers { class AAA {
}
public class BBB
{
AAA a;
}
public class Program
{
public static void Main(string[] args)
{
}
}
}
public
, private
, protected
, internal
, protected internal
. We also learnt about their priority of access and usage, let’s summarize their details in a tabular format for revision. Later, we’ll move to other types as well.Declared accessibility | Meaning |
public |
Access is not restricted. |
protected |
Access is limited to the containing class or types derived from the containing class. |
internal |
Access is limited to the current assembly. |
protected internal |
Access is limited to the current assembly or types derived from the containing class. |
private |
Access is limited to the containing type. |
protected internal
combination.internal
or public
accessibility. The default accessibility for these types is internal
.Members of | Default member accessibility | Allowed declared accessibility of the member |
enum |
Public |
None |
class |
Private |
public protected internal private protected internal |
interface |
Public |
None |
struct |
Private |
public internal private |
Sealed Classes
Sealed
” is a special class of access modifier in C#. If a class is marked as sealed
, no other class can derive from thatsealed
class. In other words, a class marked as sealed
can’t act as a base class to any other class.Program
namespace AccessModifiers { sealed class AAA {
}
class BBB:AAA
{
}
public class Program
{
public static void Main(string[] args)
{
}
}
}
Compiler Output
'AccessModifiers.BBB': cannot derive from sealed type 'AccessModifiers.AAA'
sealed
can’t act as a base class to any other class.
sealed
class.Program
using System;
namespace AccessModifiers
{
sealed class AAA
{
public int x = 100;
public void MethodA()
{
Console.WriteLine(“Method A in sealed class”);
}
}
public class Program
{
public static void Main(string[] args)
{
AAA aaa=new AAA();
Console.WriteLine(aaa.x);
aaa.MethodA();
Console.ReadKey();
}
}
}
Compiler Output
100 Method A in sealed class
sealed
and a non sealed
class is that the sealed
class cannot be derived from. A sealed
class can contain variables, methods, properties like a normal class do.sealed
classes, the code from the sealed
classes cannot be overridden.Constants
Lab1
Program
class in the console application.Program
public class Program { private const int x = 100; public static void Main(string[] args) { Console.WriteLine(x); Console.ReadKey(); } }
Output
100
const
variable at the time we create it. We are not allowed to initialize it later in our code or program.Lab2
using System;
namespace AccessModifiers
{
public class Program
{
private const int x = y + 100;
private const int y = z – 10;
private const int z = 300;
public static void Main(string[] args)
{
System.Console.WriteLine(“{0} {1} {2}”,x,y,z);
Console.ReadKey();
}
}
}
Output
390 290 300

constant
field can no doubt depend upon another constant
. C# is very smart to realize that to calculate the value of variable x
marked const
, it first needs to know the value of y
variable. y
’s value depends upon anotherconst
variable z
, whose value is set to 300
. Thus C# first evaluates z
to 300
then y
becomes 290
i.e. z -1
and finally x
takes on the value of y
i.e. 290 + 100
resulting in 390
.Lab3
Program
using System;
namespace AccessModifiers
{
public class Program
{
private const int x = y + 100;
private const int y = z – 10;
private const int z = x;
public static void Main(string[] args)
{
System.Console.WriteLine(“{0} {1} {2}”,x,y,z);
Console.ReadKey();
}
}
}
Output
The evaluation of the constant value for 'AccessModifiers.Program.x' involves a circular definition
z=x
from our previous code, and it resulted into error. The value of const x
depends upon y
, and y
in turn depends upon value of z
, but we see value z
depends upon x
as x
is assigned directly to z
, it results in a circular dependency.const
variables cannot be circular, i.e., they cannot depend on each other.Lab4
const
is a variable whose value once assigned cannot be modified, but its value is determined at compile time only.using System;
namespace AccessModifiers
{
public class Program
{
public const ClassA classA=new ClassA();
public static void Main(string[] args)
{
}
}
public class ClassA
{
}
}
Output
Compile time error: 'AccessModifiers.Program.classA' is of type 'AccessModifiers.ClassA'. A const field of a reference type other than string can only be initialized with null.
const
field of a reference type other than string
can only be initialized with null
.null
in Program
class:using System;
namespace AccessModifiers
{
public class Program
{
public const ClassA classA=null;
public static void Main(string[] args)
{
}
}
public class ClassA
{
}
}
classA
to an object which has a value that can be determined at compile time i.e., null
. We can never change the value of classA
, so it will always be null
. Normally, we do not have const
s as classA
reference type as they have value only at runtime.const
variable to a compile time value, i.e., a value available to the compiler while it is executing.new()
actually gets executed at runtime and therefore does not get value at compile time. So this results in an error.Lab5
ClassA
public class ClassA { public const int aaa = 10; }
Program
public class Program { public static void Main(string[] args) { ClassA classA=new ClassA(); Console.WriteLine(classA.aaa); Console.ReadKey(); } }
Output
Compile time error: Member 'AccessModifiers.ClassA.aaa' cannot be accessed with an instance reference; qualify it with a type name instead
static
and we can’t use the instance reference, i.e., a name to reference a const
. A const
has to be static
as no one will be allowed to make any changes to a const
variable.const
as static
.using System;
namespace AccessModifiers
{
public class ClassA
{
public static const int aaa = 10;
}
public class Program
{
public static void Main(string[] args)
{
ClassA classA=new ClassA();
Console.WriteLine(classA.aaa);
Console.ReadKey();
}
}
}
Output
Compile time error: The constant 'AccessModifiers.ClassA.aaa' cannot be marked static
static
by default cannot be marked as static
.const
variable cannot be marked as static
.Lab6
using System;
namespace AccessModifiers
{
public class ClassA
{
public const int xxx = 10;
}
public class ClassB:ClassA
{
public const int xxx = 100;
}
public class Program
{
public static void Main(string[] args)
{
Console.WriteLine(ClassA.xxx);
Console.WriteLine(ClassB.xxx);
Console.ReadKey();
}
}
}
Output
10 100 Compiler Warning: 'AccessModifiers.ClassB.xxx' hides inherited member 'AccessModifiers.ClassA.xxx'. Use the new keyword if hiding was intended.
const
with the same name in the derived class as another const
in the base class. The const
variable of class ClassB xxx
will hide the const xxx
in class ClassA
for the class ClassB
only.Static Fields
Lab1
Program
using System;
namespace AccessModifiers
{
public class Program
{
private static int x;
private static Boolean y;
public static void Main(string[] args)
{
Console.WriteLine(x);
Console.WriteLine(y);
Console.ReadKey();
}
}
}
Output
0 False
Static
variables are always initialized when the class is loaded first. An int
is given a default value of zero and a bool
is given a default to False
.Lab2
Program
using System;
namespace AccessModifiers
{
public class Program
{
private int x;
private Boolean y;
public static void Main(string[] args)
{
Program program=new Program();
Console.WriteLine(program.x);
Console.WriteLine(program.y);
Console.ReadKey();
}
}
}
Output
0 False
new
will create an instance of the class Program
. It will allocate memory for each of the non static
, i.e. instance variables and then initialize each of them to their default values as well.Lab3
Program
using System;
namespace AccessModifiers
{
public class Program
{
private static int x = y + 10;
private static int y = x + 5;
public static void Main(string[] args)
{
Console.WriteLine(Program.x);
Console.WriteLine(Program.y);
Console.ReadKey();
}
}
}
Output
10 15
static
variables to their initial value after creating them. Variables x
and y
are therefore given a default of zero value. C# now realizes that these variables declared need to be assigned some values. C# does not read all the lines at once but only one at a time. It will now read the first line and as the variable y
has a value of 0
, so x
will get a value of 10
. Then at the next line, y
is the value of x + 5
. The variable x
has a value of 10
and so y
now becomes 15
. As C# does not see both lines at the same time, it does not notice the circularity of the above definition.Lab4
Program
using System;
namespace AccessModifiers
{
public class Program
{
int x = y + 10;
int y = x + 5;
public static void Main(string[] args)
{
}
}
}
Output
Compile time error:
A field initializer cannot reference the non-static field, method, or property ‘AccessModifiers.Program.y’
A field initializer cannot reference the non-static field, method, or property ‘AccessModifiers.Program.x’
Lab3
does not work for instance variables as the rules of an instance variable are quite different than that of static
variables. The initializer of an instance variable has to be determined at the time of creation of the instance. The variable y
does not have a value at this point in time. It can’t refer to variables of the same object at the time of creation. So we can refer to no instance members to initialize an instance member.Readonly Fields
Readonly
fields are one of the most interesting topics of OOP in C#.Lab1
Program
using System;
namespace AccessModifiers
{
public class Program
{
public static readonly int x = 100;
public static void Main(string[] args)
{
Console.WriteLine(x);
Console.ReadKey();
}
}
}
Output
100
static
variable inside a static
method, else we’ll get an error.Lab2
Program
using System;
namespace AccessModifiers
{
public class Program
{
public static readonly int x = 100;
public static void Main(string[] args)
{
x = 200;
Console.WriteLine(x);
Console.ReadKey();
}
}
}
Output
Compile time error: A static readonly field cannot be assigned to (except in a static constructor or a variable initializer).
readonly
field except in a constructor.static readonly
field cannot be assigned to (except in a static
constructor or a variable initializer).Lab3
Program
using System;
namespace AccessModifiers
{
public class Program
{
public static readonly int x;
public static void Main(string[] args)
{
}
}
}
const
and readonly
, unlike const
, readonly
fields need not have to be initialized at the time of creation.Lab4
Program
using System;
namespace AccessModifiers
{
public class Program
{
public static readonly int x;
static Program()
{
x = 100;
Console.WriteLine(“Inside Constructor”);
}
public static void Main(string[] args)
{
Console.WriteLine(x);
Console.ReadKey();
}
}
}
Output
Inside Constructor 100
const
and readonly
is seen here. A static readonly
variable can be initialized in the constructor as well, like we have seen in the above mentioned example.Lab5
Program
using System;
namespace AccessModifiers
{
public class ClassA
{
}
public class Program
{
public readonly ClassA classA=new ClassA();
public static void Main(string[] args)
{
}
}
}
const
section. The same code gave an error with const
does not give an error with readonly fields. So we can say that readonly
is a more generic const
and it makes our programs more readable as we refer to a name and not a number. Is 10
more intuitive or priceofcookie
easier to understand? The compiler would for efficiency convert all const
s and readonly
fields to the actual values.Lab6
Program
using System;
namespace AccessModifiers
{
public class ClassA
{
public int readonly x= 100;
}
public class Program
{
public static void Main(string[] args)
{
}
}
}
Output
Compile time error:
Member modifier ‘readonly’ must precede the member type and name
Invalid token ‘=’ in class, struct, or interface member declaration
readonly
modifier precedes the data type int
, we already discussed at the very start of the article. This is just a rule that must always be remembered.Lab7
Program
using System;
namespace AccessModifiers
{
public class ClassA
{
public readonly int x= 100;
void Method1(ref int y)
{
}
void Method2()
{
Method1(ref x);
}
}
public class Program
{
public static void Main(string[] args)
{
}
}
}
Output
Compile time error:
A readonly field cannot be passed ref or out (except in a constructor)
A readonly field can’t be changed by anyone except a constructor.
The method Method1 expects a ref parameter which if we have forgotten allows you
to change the value of the original. Therefore C# does not permit a readonly
as a parameter to a method that accepts a ref or an out parameters.
Summary
- The default access modifier is
private
for class members. - A class marked as
internal
can have its access limited to the current assembly only. - Namespaces as we see by default can have no accessibility specifiers at all. They are by default
public
and we cannot add any other access modifier includingpublic
again too. - A class can only be
public
orinternal
. It cannot be marked asprotected
orprivate
. The default isinternal
for the class. - Members of a class can be marked with all the access modifiers, and the default access modifier is
private
. Protected internal
means that the derived class and the class within the same source code file can have access.- Between
public
andinternal
,public
always allows greater access to its members. - Base class always allows more accessibility than the derived class.
- The return values of a method must have greater accessibility than that of the method itself.
- A class marked
sealed
can’t act as a base class to any other class. - Since we cannot derive from
sealed
classes, the code from thesealed
classes cannot be overridden. - We need to initialize the
const
variable at the time we create it. We are not allowed to initialize it later in our code or program. - Like classes,
const
variables cannot be circular, i.e., they cannot depend on each other. - A
const
field of a reference type other thanstring
can only be initialized withnull
. - One can only initialize a
const
variable to a compile time value, i.e., a value available to the compiler while it is executing. - A constant by default is
static
and we can’t use the instance reference, i.e., a name to reference aconst
. Aconst
has to bestatic
as no one will be allowed to make any changes to aconst
variable. - A
const
variable cannot be marked asstatic
. - A variable in C# can never have an uninitialized value.
Static
variables are always initialized when the class is loaded first. Anint
is given a default value of zero and abool
is given a default ofFalse
.- An instance variable is always initialized at the time of creation of its instance.
- A
static readonly
field cannot be assigned to (except in astatic
constructor or a variable initializer).
Conclusion

Read more:
- C# and ASP.NET Questions (All in one)
- MVC Interview Questions
- C# and ASP.NET Interview Questions and Answers
- Web Services and Windows Services Interview Questions
Other Series
My other series of articles:
For more informative articles visit my Blog.
7 comments