C# and .NET interview questions: – Elaborate differentiation between Constant and ReadOnly in C#?

Const: – The const keyword is used to modify a declaration of a field
or local variable. It specifies that the value of the field or the local
variable cannot be modified.

Example: public const double x = 1.0;
public const int y=10;

ReadOnly: – The readonly keyword is a modifier that you can use on
fields. When a field declaration includes a readonly modifier, assignments to the fields introduced by the declaration can only occur as part of the declaration or in a constructor in the same class.

Example: public readonly int y = 5;
public readonly string x;

Both Constant and ReadOnly keywords are used to deal with constant value but the difference in both is that the value of readonly variables is set at runtime, so it can have different value for different execution of the program. Whereas the value of constant variables is set during compile time.

Let’s see a small demonstration to prove the above difference between the declarations of both the keywords.

Let me first define a variable using Constant keyword without assigning any value to it and later try to compile the code and see that what the compiler has to say about it.

public class MyClass
{
public const double JanDays;
}

In the above code snippet you can clearly see that I have created a variable as constant without assigning value to it, now let’s try to compile it.

Now, let’s see a simple example of creating a variable with readonly keyword without assigning value to it and try to compile it and see that what the compiler has to say about it.

public class MyClass
{
public readonly double FebuaryDays;
}

In the above code snippet you can see that I have declared variable using readonly keyword without assigning value to it, now let’s try to compile it.

From the above diagram it’s clearly clear that the variables which are defined with readonly keyword can have value during execution time but the value for the variables which are defined with constant keyword should have value during compile time only.

When you create a variable with using readonly keyword this means that the value to that variable can only be given inside the constructor.

Let’s see a small demonstration to prove the above point.

public class MyClass
{
public readonly int FebDays;

public void MyMethod()
{
FebDays = 20;
}
}

In the above code you can see that I have created a variable using readonly keyword and also created a method “MyMethod” which is assigning value to the readonly variable. Now, let’s try to compile the code and see that what the compiler has to say about this.

In the above diagram you can clearly see that the compiler throws an error. Which means the value to the readonly fields cannot be assigned inside the method.

Now, let’s try to assign a value to the readonly variable through the
constructor and see that what the compiler has to say about that.

public class MyClass
{
public readonly int FebDays;

public MyClass()
{
FebDays = 29;
}
}

In the above code snippet you can see that I have created a constructor which is assigning value to the readonly variable. Now let’s compile the code and see what the compiler says.

From the above diagram it’s clear that you can assign value to the readonly variable in the constructor.

Watch video on different types of collection in .NET and C# as follows: –

Get more materials on important .NET interview questions

Regards,

See more stuffs on author’s blog for Most asked .NET interview questions

Advertisements

About c# and .NET Interview questions

This blog is for developers who want to crack .NET and C# interviews. It has all tips and tricks needed to crack .NET interviews , C# interview , SQL Server interview , Java interview , WCF Interview , Silverlight interview , WPF interview , LINQ interview , Entity framework Interview. Do not forget to watch our Learn step by step video series. Learn MVC in 16 hours:- https://www.youtube.com/watch?v=Lp7nSImO5vk Learn AngularJS Step by Step:- https://www.youtube.com/watch?v=0kmdjqgO9IY Learn Design Pattern in 8 hours:- https://www.youtube.com/watch?v=YDobmucohqk Learn C# and .NET in 60 days:- https://www.youtube.com/watch?v=yh2SrzCkNQA Learn MSBI in 32 hours:- https://www.youtube.com/watch?v=mGPJx3ocFgg Learn SharePoint Step by Step in 8 hours:- https://youtu.be/C2fW76SwJNU
This entry was posted in Uncategorized and tagged , , , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s