Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...

What is the Difference between read only and constant
variables?

Answer Posted / sandya

Constant fields or local variables must be assigned a value at the time of declaration and after that they cannot be modified. By default constant are static, hence you cannot define a constant type as static.
A const field is a compile-time constant. A constant field or local variable can be initialized with a constant expression which must be fully evaluated at compile time.
public const int X = 10;
A readonly field can be initialized either at the time of declaration or with in the constructor of same class. Therefore, readonly fields can be used for run-time constants.

class MyClass
{
readonly int X = 10; // initialized at the time of declaration
readonly int X1;
}

public MyClass(int x1)
{
X1 = x1; // initialized at run time
}
}

Is This Answer Correct ?    0 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

what is an exception in .net?

896


What does it mean to override a method?

901


How many bytes is an int c#?

836


Is c# a keyword?

879


What is the diff between System.String and System.Text.StringBuilder classes?

956


Is lazy t thread safe?

897


What is the max value of int32 in c#?

861


Can an exception be thrown from a catch block?

963


What are the examples of data types?

929


What is sqldataadapter in c#?

855


Can you declare struct members as protected?

856


What is the difference between struct and class in c#?

1150


How is a strongly-named assembly different from one that isn’t strongly-named?

992


Why to use “finally” block in c#?

1029


What is a must for multitasking

927