Click here to Skip to main content
15,886,724 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
Generally constant variables intalized at the time of declartion.it has fixed value can not be change but radonly varibles intalized either at the time of declartion or constructor also but that is it change the varibles during program execution .i want about both varibles intaliztion.is it changed or not



please help me.

thank u.

What I have tried:

Generally constant variables intalized at the time of declartion.it has fixed value can not be change but radonly varibles intalized either at the time of declartion or constructor also but that is it change the varibles during program execution .i want about both varibles intaliztion.is it changed or not
Posted
Updated 24-Feb-17 9:10am
v2
Comments
Maciej Los 24-Feb-17 12:52pm    
Well, you can check it out.
Krishna Veni 24-Feb-17 13:01pm    
readonly varibles changed during runtime(program excution) but how readonly same as constant and also static modifer allows for readonly varibles.pls reply
[no name] 24-Feb-17 13:19pm    
I have no idea what you are really asking. But it seems to me that Maciej is on the right track. You could easily try it yourself.
Maciej Los 24-Feb-17 13:53pm    
I have no idea too! :laugh:

I think this article explains what you want to know: Const vs Static vs Readonly in C#[^]
 
Share this answer
 
Comments
Maciej Los 24-Feb-17 13:46pm    
5ed!
Another useful article can be found here: Constant vs Readonly vs Static Keywords in C#[^]
and here: Practical Difference between Const & ReadOnly[^]

For further details, please see:
Properties Tutorial (C#)[^]
Using Properties (C# Programming Guide)[^]

As to the readonly properties of class...
Use it when you want to prevent user to change value returned by this property. Example:
C#
void Main()
{
	Person p = new Person("Maciej", "Los", new DateTime(1968,1,1));
	Console.WriteLine("Person details: {0} {1}, age: {2}", p.FirstName, p.LastName, p.Age);
	p.DateOfBirth = new DateTime(1970,2,12);
	Console.WriteLine("Person details: {0} {1}, age: {2}", p.FirstName, p.LastName, p.Age);
}

// Define other methods and classes here
public class Person
{
	private string sFName = string.Empty;
	private string sLName = string.Empty;
	private DateTime dDob = new DateTime(1900,1,1);
	
	public Person(string _FName, string _LName, DateTime _DateOfBirth)
	{
		sFName = _FName;
		sLName = _LName;
		dDob = _DateOfBirth;
	}
	
	public string FirstName
	{
		get { return sFName; }
		set { sFName = value; }
	}
	
	public string LastName
	{
		get { return sLName; }
		set { sLName = value; }
	}
	
	public DateTime DateOfBirth 
	{
		get { return dDob; }
		set { dDob = value; }
	}
	
	public int Age
	{
		get{ return DateTime.Today.Year - dDob.Year; }
	}
	
}

Above code returns:
Person details: Maciej Los, age: 49
Person details: Maciej Los, age: 47
 
Share this answer
 
Comments
Graeme_Grant 24-Feb-17 18:03pm    
1968 was a good year ;)
Maciej Los 25-Feb-17 4:01am    
:laugh:
Cheers!
Maciej

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900