C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !






Differences between const and readonly


Posted Date: 20 Jun 2008    Resource Type: Articles    Category: .NET Framework

Posted By: Gaurav Agrawal       Member Level: Silver
Rating:     Points: 10



This article explains the differences between a "const" and a "readonly" variable with examples (in C#)

A Const field can only be initialized at the declaration of the field.

For Example:


public class class1
{
const int valu11 = 5; // Declared a Const and assigned a value to it
}


But when we try to do something, as in this code sample


public class class1
{
const int valu11 = 5;
public class1 // Constructor
{
value1 = 10; // Error
}
}


In the above example, we have assigned a new value to constant and .NET does not allow this because we cannot change the value of the constant field throughout the program.

A const field is a compile-time constant while a readonly field has two point at which it can be - initialized either at the declaration or in a constructor. Therefore, readonly fields can have different values depending on the constructor used.

Example:


public class class1
{
readonly int valu11 = 5;// Declared a readonly and assigned a value to it
}

public class class1
{
readonly int valu11 = 5;
public class1 // Constructor
{
value1 = 10; // Allowed
}
public void method1()
{
value1 = 15 // Error
}
}


We can change the value of readonly field as many times as we want but only inside a constructor and if we try to change it outside the constructor (in any method or any other location)then program will not compile and will show a error like that a readonly field can not be assigned to(execpt in a constructor or a variable intializer.)

The readonly field can be used for runtime constants also.




Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Difference between redonly and const  .  Difference between const and readonly  .  Const and readonly differences  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: View State Chunking in ASP.NET
Previous Resource: Compile Time Polymorphism -- Function Overloading
Return to Discussion Resource Index
Post New Resource
Category: .NET Framework


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design

web conferencing

Contact Us    Privacy Policy    Terms Of Use