V24s

"Get Updates : Subscribe to our e-mail newsletter to receive updates........" "Good Luck frnds" Admission 2019 Application Form 2019 Colleges Entrance Exam 2019 Results 2019 Notification 2019 University Educational Jobs 2019 Government Jobs 2019

Search This Blog v24s guys

15 April, 2013

C# - What is the Difference between Const and Readonly

Introduction:

Here I will explain what are the differences between const and readonly in c#.net and vb.net.

Description:

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 inc#.net and vb.net.

Const:

      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.

Read-only:

     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.
        
Example

public class Const_VS_Readonly
{
public const int I_CONST_VALUE = 2;
public readonly int I_RO_VALUE;
public Const_VS_Readonly()
{
I_RO_VALUE = 3;
}
}
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.

No comments:

Popular Posts

Recent Posts

Google Analytics