public class Const_VS_Readonly
public const int I_CONST_VALUE = 2;
public readonly int I_RO_VALUE;
I_RO_VALUE = 3;
By: Suresh Dasari Oct 31, 2012
In previous posts I explained convert.tostring() VS .tostring(), web application VS website, difference between datareader, dataadapter and dataset, difference between tinyint, smallint, int, bigint and many articles relating to asp.net, SQL Server. Now I will explain the difference between const and readonly in c#.net and vb.net.
1. Const can only be initialized at the time of declaration of the field.
2. Const values will evaluate at compile time only.
3. Const value can’t be changed these will be same at all the time.
4. This type of fields are required when one of the field values remains constant throughout the system like Pi will remain same in your Maths Class.
1. The value will be initialized either declaration time or the constructor of the class allowing you to pass the value at run time.
2. Read only values will evaluate at runtime only.
Suppose if you want the value of the constant won't change use a const or if you have a constant that may change or when in doubt, use a readonly. I hope it helps.
Other Related Posts
- Interview Questions in ASP.NET,C#.NET,SQL Server,.NET Framework
- Asp.net insert, Edit, update, delete data in gridview
- 3 tier architecture example in asp.net with C#
- Introduction to Object Oriented Programming Concepts (OOPS) in C#.net
- Simple login form example in asp.net Check Username and Password availability in database
- Introduction to WCF - WCF tutorial | WCF Tutorial - Windows Communication Foundation | WCF Example | WCF Sample code in asp.net 3.5 | Basic WCF Tutorial for Beginners
- how to insert images into database and how to retrieve and bind images to gridview using asp.net (or) save and retrieve images from database using asp.net
- jQuery 360 Degrees Image Display Plugins Examples with Tutorial
- Best Login Page Design in HTML, CSS with Source Code
- Asp.net WebService or Creating and Consuming WebService in asp.net or Create and call webservice in asp.net or how to create webservice and how to use webservice in asp.net