属性是类的成员,用于封装字段(变量)。它们提供了对字段的访问控制,并可以在读取或设置字段值时执行额外的逻辑(如验证、转换等)。属性是实现封装的重要手段,能够隐藏类的内部实现细节,同时提供对外部的接口。
属性的语法通常包括get和set访问器:
假设有一个Person类,包含Name和Age属性:
public class Person
{
private string name; // 私有字段
private int age; // 私有字段
// 读写属性
public string Name
{
get { return name; }
set { name = value; }
}
// 读写属性,带验证逻辑
public int Age
{
get { return age; }
set
{
if (value < 0)
{
throw new ArgumentException("年龄不能为负数");
}
age = value;
}
}
}
Person person = new Person();
person.Name = "Alice"; // 设置属性
person.Age = 30; // 设置属性
Console.WriteLine(person.Name); // 获取属性
Console.WriteLine(person.Age); // 获取属性
public class Person
{
public string Name { get; set; } // 自动实现的属性
public int Age { get; set; } // 自动实现的属性
}
索引器允许对象以类似数组的方式被索引。它提供了一种灵活的方式来访问对象的内部数据,而不需要暴露内部实现细节。索引器特别适用于集合类或需要通过索引访问的复杂对象。
索引器的语法类似于数组的访问方式,但可以在get和set访问器中实现自定义逻辑。
假设有一个PersonCollection类,用于存储Person对象,并通过索引器访问它们:
public class PersonCollection
{
private List<Person> people = new List<Person>();
// 索引器
public Person this[int index]
{
get
{
if (index < 0 || index >= people.Count)
{
throw new ArgumentOutOfRangeException("索引超出范围");
}
return people[index];
}
set
{
if (index < 0 || index >= people.Count)
{
throw new ArgumentOutOfRangeException("索引超出范围");
}
people[index] = value;
}
}
// 添加Person
public void Add(Person person)
{
people.Add(person);
}
}
PersonCollection collection = new PersonCollection();
collection.Add(new Person { Name = "Alice", Age = 30 });
collection.Add(new Person { Name = "Bob", Age = 25 });
// 使用索引器访问
Person person = collection[0]; // 获取第一个人
Console.WriteLine(person.Name); // 输出:Alice
// 使用索引器修改
collection[1] = new Person { Name = "Charlie", Age = 28 };
多参数索引器允许通过多个参数访问对象的内部数据。这在处理多维数据或复杂对象时非常有用。多参数索引器可以通过params关键字或直接定义多个参数来实现。
假设有一个Matrix类,用于存储二维数据,并通过多参数索引器访问:
public class Matrix
{
private int[,] data;
public Matrix(int rows, int cols)
{
data = new int[rows, cols];
}
// 多参数索引器
public int this[int row, int col]
{
get
{
if (row < 0 || row >= data.GetLength(0) || col < 0 || col >= data.GetLength(1))
{
throw new ArgumentOutOfRangeException("行或列超出范围");
}
return data[row, col];
}
set
{
if (row < 0 || row >= data.GetLength(0) || col < 0 || col >= data.GetLength(1))
{
throw new ArgumentOutOfRangeException("行或列超出范围");
}
data[row, col] = value;
}
}
}
Matrix matrix = new Matrix(3, 3);
// 设置值
matrix[0, 0] = 1;
matrix[1, 1] = 2;
matrix[2, 2] = 3;
// 获取值
Console.WriteLine(matrix[0, 0]); // 输出:1
Console.WriteLine(matrix[1, 1]); // 输出:2
Console.WriteLine(matrix[2, 2]); // 输出:3
通过掌握属性和索引器的使用,你可以更好地封装类的内部数据,同时提供灵活的接口供外部使用。希望这些内容能帮助你更深入地理解和应用这些强大的C#特性!