C#属性访问器

时间:2024-07-17 12:35:08

属性的访问器包含与获取或设置属性有关的可执行语句。访问器声明可以包含 get 访问器或 set 访问器,或者两者均包含。声明采用下列形式之一:
get {}
set {}

get 访问器
get 访问器体与方法体相似。它必须返回属性类型的值。执行 get 访问器相当于读取字段的值。以下是返回私有字段 name 的值的 get 访问器:

private string name;  // the name field
public string Name // the Name property
{
get
{
return name;
}
}

当引用属性时,除非该属性为赋值目标,否则将调用 get 访问器读取该属性的值。例如:

  Employee e1 = new Employee();
  ...
  Console.Write(e1.Name); // The get accessor is invoked here
get 访问器必须在 return 或 throw 语句中终止,并且控制不能超出访问器体。

set 访问器
set 访问器与返回 void 的方法类似。它使用称为 value 的隐式参数,此参数的类型是属性的类型。在下例中,set 访问器被添加到 Name 属性:

public string Name
{
get
{
return name;
}
set
{
name = value;
}
}

当对属性赋值时,该操作将调用 set 访问器。例如:

e1.Name = "Joe"; // The set accessor is invoked here
在 set 访问器中对局部变量声明使用隐式参数名 (value) 是错误的。

备注
属性按如下方式,根据所使用的访问器进行分类:
只带有 get 访问器的属性称为只读属性。无法对只读属性赋值。
只带有 set 访问器的属性称为只写属性。只写属性除作为赋值的目标外,无法对其进行引用。
同时带有 get 和 set 访问器的属性为读写属性。
在属性声明中,get 和 set 访问器都必须在属性体的内部声明。
使用 get 访问器更改对象的状态是一种错误的编程样式。

====

我们通过下面的例子来认识什么是访问器:

 using System;

 namespace AccessorEG
{
public class Student
{
// 私有字段 private field
private int _age; // 公开的属性 public property
public int Age
{
get { return _age; }
set { _age = value; }
}
} class Program
{
static void Main(string[] args)
{
Student stu = new Student();
stu.Age = ; // 使用了修改
Console.WriteLine(stu.Age.ToString()); // 使用了读取
Console.ReadKey();
// 输出 10
}
}
}

  很好理解,访问器就是指对象类型成员对外界的接口,就是使对象类型成员与外界进行信息交互的桥梁,有了访问器,外界就能对对象成员进行读、写的对应操作。

  那么,什么成员能够拥有访问器呢?非只读的字段和事件是可以声明访问器的。当然,只读域也能提供被外界获取的接口,即get,但是只能在声明或构造函数中初始化,而且它并不支持提供set方法。

 using System;

 namespace AccessorEG
{
public class Student
{
// 私有字段 private field
private readonly int _age = ; // 公开的属性 public property
public int Age
{
get { return _age; }
}
} class Program
{
static void Main(string[] args)
{
Student stu = new Student();
Console.WriteLine(stu.Age.ToString()); // 使用了读取
Console.ReadKey();
// 输出 10
}
}
}

  上述代码中只读域的值在声明时就已经赋了,而它对应公开属性的访问器中也不能提供set方法,不然会无法通过编译,但是它可以被外界取得。

  关于字段的访问器我们还要说一些,常见的有以下写法:

using System;

 namespace AccessorEG
{
public class Student
{
#region 全访问权限
// 私有字段
private int _age;
// 与_age对应的公开属性,包含了set和get方法
public int Age
{
get { return _age; }
set { _age = value; }
} // 如果您安装了.NET3.0,那么您可以使用自动属性,届时,上面的代码即可以下面的代替
     // 在VS.NET下输入 prop 连击两下Tab键,编译器会自动帮您生成自动属性
     // public int Age { get; set; }
#endregion // 全访问权限 #region 只读属性
private string _name; public string Name
{
get { return _name; }
} // 等同于
     // public string Name { private set; get; }
#endregion #region 只写属性
private bool _sex; public bool Sex
{
set { _sex = value; }
}
// 等同于
     // public bool Sex { set; private get; }
#endregion } class Program
{
static void Main(string[] args)
{
Student stu = new Student();
stu.Age = ;
// stu.Name = "Johness"; 异常,编译错误,因为该属性只读
       // Console.WriteLine(stu.Sex.ToString()); 异常,编译错误,因为该属性只写
Console.WriteLine(stu.Age.ToString()); // 使用了读取
Console.ReadKey();
// 输出 18
}
}
}

  以上示例中的只读、只写仅对外界有效,如果您显示得制定了该访问器的所有者,即类的私有字段。那么在类的内部,您仍可以方便的使用您定义的私有字段进行读写操作,因此,我建议朋友们定义字段及其访问器使用.NET2.0的语法而不用3.0的新语法(自动属性)。当然,利用访问器也能更好地对数据有效性进行验证:

 using System;

 namespace AccessorEG
{
public class Student
{
// 私有字段
private int _age;
// 与_age对应的公开属性,包含了set和get方法
public int Age
{
get { return _age; }
// 利用访问器对输入的年龄进行验证
       // 如果输入值小于0或者大于100
       // 可以赋为默认值18或者不进行操作
set
{
if (value >= && value <= )
_age = value;
// 如果数据无效不进行操作可以注释以下内容
else
_age = ;
}
} } class Program
{
static void Main(string[] args)
{
Student stu = new Student();
stu.Age = -; // 赋无效值
Console.WriteLine(stu.Age.ToString());
Console.ReadKey();
// 输出 18
}
}
}