如何在C#中添加只读属性?
标记为“只读”的字段只能在构造对象期间设置一次。它不能更改-
让我们来看一个例子。
class Employee {
readonly int salary;
Employee(int salary) {
this.salary = salary;
}
void UpdateSalary() {
//薪水=50000;//编译错误
}
}上面,我们将薪金字段设置为只读。
如果要更改它,则会发生编译时错误。在上面的示例中也是如此。
现在让我们看看如何检查数组是否为只读-
示例
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace lower {
class Program {
static void Main(string[] args) {
Array arr = Array.CreateInstance(typeof(String), 3);
arr.SetValue("Maths", 0);
arr.SetValue("Science", 1);
arr.SetValue("PHP", 2);
Console.WriteLine("isReadOnly: {0}",arr.IsReadOnly.ToString());
}
}
}热门推荐
10 财运虎年祝福语简短语
11 单亲爸爸春节祝福语简短
12 狗狗上厕所训练
13 植物种植祝福语简短
14 翻译考试祝福语朋友简短
15 个人周年庆简短祝福语
16 狗狗怎么训练
17 分别祝福语简短搞笑英文
18 生日广州男生祝福语简短