它是否改变了只读变量 [英] Is it change the readonly varibles

查看:88
本文介绍了它是否改变了只读变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通常在declartion.it时具有固定值的常量变量具有固定值不能更改但是在声明或构造函数时也可以使用无限变量,但是它会在程序执行期间更改变量。我想要两个varibles intaliztion.is它改变了吗







请帮帮我。



谢谢你。



我的尝试:



通常在declartion.it时具有固定值的常量变量具有固定值不能更改,但是在声明或构造函数时也可以使用无限变量进行内部化,但是它会在程序执行期间更改变量。我想要关于varibles intaliztion.is它是否改变

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

推荐答案

我认为这篇文章解释了你想知道的: Const vs St. atic vs Readonly in C# [ ^ ]
I think this article explains what you want to know: Const vs Static vs Readonly in C#[^]


另一篇有用的文章可以在这里找到: Constant vs Readonly vs C#中的静态关键字 [ ^ ]

和此处: Const& amp; ReadOnly [ ^ ]



详情请见:

属性教程(C#) [ ^ ]

使用属性(C#编程指南) [ ^ ]



关于班级的只读属性...

如果要阻止用户更改此属性返回的值,请使用它。示例:

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


这篇关于它是否改变了只读变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆