Click here to Skip to main content
14,639,066 members
Rate this:
Please Sign up or sign in to vote.
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:
Rate this:
Please Sign up or sign in to vote.

Solution 1

I think this article explains what you want to know: Const vs Static vs Readonly in C#[^]
   
Comments
Maciej Los 24-Feb-17 13:46pm
   
5ed!
Rate this:
Please Sign up or sign in to vote.

Solution 2

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:
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
   
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, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100