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?

Answers were Sorted based on User's Feedback



What is the Difference between read only and constant variables?..

Answer / 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

What is the Difference between read only and constant variables?..

Answer / bharathi

Constants:
constant is used for declaring variable as constant and this constant value cannot be modified once after declartration.

It is must to declare the constant value at the time of declaration only.
Example:
constant int y=5;

Readonly

Readonly variables can not be modified like constant,but it is after initialization.
So.it is not mandatory to initialize those variables at declaration time.they can also be initialize after declaration.

Example:
readonly int x;

add()
{
x=5;
}


Both after initilization, we can not modify those values again.

Is This Answer Correct ?    0 Yes 0 No

What is the Difference between read only and constant variables?..

Answer / isaiselvan

readonly assign a value at run time but we can assign a declaration part.
const values assign only at declaration part.

Is This Answer Correct ?    2 Yes 3 No

What is the Difference between read only and constant variables?..

Answer / zia khan

The difference between the readonly and const data members
is that const requires you to initialize with the
declaration, that is directly

Is This Answer Correct ?    4 Yes 6 No

Post New Answer

More C Sharp Interview Questions

What is hashtable in c# with example?

0 Answers  


What is variable and its classification?

0 Answers  


What is a console file?

0 Answers  


what is the difference between passing a value object by reference and a reference object by value?

4 Answers   TCS,


What do you mean by hashtable c#?

0 Answers  


What’s thread.sleep() in threading ?

0 Answers  


What is the wildcard character in sql?

0 Answers  


Difference between value and reference type. What are value types and reference types?

0 Answers  


How do I calculate relative time?

0 Answers  


explain the three services model commonly know as a three-tier application.

0 Answers   Siebel Systems,


Explain About Assembly in .NET, types of assemblies, their difference, How to register into GAC. How to generate the strong names & its use.

0 Answers  


what is overloading & overriding

3 Answers   Fulcrum Logic, Synergy,


Categories