What is the difference between const and readonly in .NET?

The programmer want to link together pages with previous and next page functionality, you had to insert link manually for this to work. Whenever a page was added or inserted, you had to adjust links in two more files.



Const
ReadOnly
Value of const identifiers cannot be changed. User cannot change value of ReadOnly identifiers but they can be changed by themselves.
Const identifiers are static members by default. Here is an example defining and using Const:
public class sampleClass{
public const int sample = 10;
public static void Main() {
Console.WriteLine("sampleClass.sample
is {0}", sampleClass.sample);
}
}
Output of this code will be:
sampleClass.sample is 10
In the above example, if you try to access sample using an instance of sampleClass named obj, then you will get the following error:
Static member 'sampleClass.sample' cannot be accessed with an instance reference; qualify it with a type name instead
Readonly identifiers are instance members.
Here is an example for readonly:
public class sampleClass{
public readonly int sample = 10;
public static void Main() {
sampleClass obj = new
sampleClass();
Console.WriteLine("obj.sample is {0}",
obj.sample);
}
}
Output of this code will be:
obj.sample is 10
You will get the following error if you try to access sample as sampleClass.sample:
An object reference is required for the nonstatic field, method, or property 'sampleClass.sample'
Const identifiers are compile time constants. They cannot be initialized at run time. ReadOnly identifiers are run time constants. They can be initialized inside constructor of the class during run time.

 

| Can you call a constructor from another constructor of the Class in .NET? | Difference between Response.Output.Write() method and Response.Write() method in .NET | How do you establish multiple inheritance in C#? | How do you introduce a ReadOnly property in C#? | How do you perform constructor overloading in C#? | Is catch(Exception) recommended to be used in .NET? | What are the different access modifiers available in C#? | What are the different ways of overloading in C#? | What are the members of stringbuilder class in C#? | What is Multicast Delegate? Explain it with example in C# | What is the difference between abstract class and interface in .NET? | What is the difference between Clone and CopyTo methods in .NET | What is the difference between const and readonly in .NET | What is the difference between directcast and ctype in .NET? | What is the difference between out and ref parameters in .NET | What is the difference between public assembly and private assembly in .NET | What is the difference between strong typing and weak typing in .NET? | What is the difference between Trace and Debug in .NET | What is the need for Abstract Factory Pattern in C#? | What is the need for Adapter Pattern in C# |


“Amazon and the Amazon logo are trademarks of Amazon.com, Inc. or its affiliates.”

| Privacy Policy for www.dotnet-guide.com | Disclosure | Contact |

Copyright - © 2004 - 2024 - All Rights Reserved.