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.
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.
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.
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.
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 interview questions and answers for .NET
Regards,
See more stuffs on author’s blog for Most asked Dotnet interview question
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 interview questions and answers for .NET
Regards,
See more stuffs on author’s blog for Most asked Dotnet interview question
1 comment:
why constructor do not have return type??
Post a Comment