试图理解 C# 中的索引器
Trying to understand indexers in C#
我正在努力理解 indexers in C#。在接受的答案中,我无法理解如何:
public float this[int index]
{
get{
return temps[index];
}
set{
temps[index] = value;
}
}
有效。如果我有 2 个浮点变量怎么办?
简而言之,索引器是一种技术,您可以使用它来允许 class 以类似于数组的方式运行,允许您使用索引访问值。
- 一般来说,只有当 class 在概念上应该表现得像数组时,您才会这样做,它的主要功能是保存其他对象的单个集合。
一个常见的情况是 class 是一个 wrapper,用于您不想直接公开的内部数组变量。我们尽量避免 classes 从数组或 IEnumerable 继承,因为有很多功能要实现,但为了添加额外功能而包装数组可能很有意义。
temps
在你的语法中 public float temps this[int index]
没有意义,也不是必需的,因为 class 只能有每个 type[ 的单个索引器=47=].
如果 temps
字段公开为 public,而不是使用索引器,那么您可以使用此语法来访问索引 5 的值:
TempratureRecord oTest = new TemperatureRecord();
var tempAtIndex5 = oTest.temps[5];
但现在您可以访问 temps
变量本身,这意味着您可以重新分配它。
Do not mistake that example as the only way to expose values from internal arrays, if you had multiple arrays then you can expose them in different ways, generally NOT by using an indexer.
如果索引器仅公开一个内部数组,但不提供任何其他内容,那么这可能根本不是使用它们的好理由。
你可以有一个 string
类型的索引器和一个单独的 int
类型的索引器,如果内部数组是一个可能同时具有唯一性的对象数组,这是一种常见的模式string 和 int 属性 可用于识别 return.
的正确对象
但是,您通常不会仅仅因为有一个整数数组和一个字符串数组就这样做。 Indexers 的一般期望是,如果有多个索引器,我们会为调用者提供一种不同的方式来访问相同的 conceptual 对象列表,并且实际上以不同的方式定位相同的对象,也许是因为我们 不知道 该对象在内部数组中的索引。
一个示例,说明如何使用多个索引器访问同一个内部数组,但显示传入的索引不必是内部使用的索引:
注意:通过这种方式,允许 setter 可能根本没有意义
public class Person
{
string Name { get; set; }
int Id { get; set; }
}
public class People
{
private Person[] _backingStore;
/// <summary>Instantiate a new list of people</summary>
public People(params Person[] persons)
{
_backingStore = persons;
}
/// <summary> Return a person from this list by Id </summary>
public Person this[int id]
{
get{
return _backingStore.Where(p => p.Id == id).FirstOrDefault();
}
}
/// <summary> Return a person from this list by Name </summary>
public Person this[string name]
{
get{
return _backingStore.Where(p => p.Name == name).FirstOrDefault();
}
}
}
...
People list = new People(
new Person(){ Id = 26, Name = "Hassan" },
new Person(){ Id = 101, Name = "John Skeet" }
);
var john = list["John Skeet"];
var alsoJohn = list[101];
var no1 = list["someone else"];
var hassan = list["Hassan"];
我正在努力理解 indexers in C#。在接受的答案中,我无法理解如何:
public float this[int index]
{
get{
return temps[index];
}
set{
temps[index] = value;
}
}
有效。如果我有 2 个浮点变量怎么办?
简而言之,索引器是一种技术,您可以使用它来允许 class 以类似于数组的方式运行,允许您使用索引访问值。
- 一般来说,只有当 class 在概念上应该表现得像数组时,您才会这样做,它的主要功能是保存其他对象的单个集合。
一个常见的情况是 class 是一个 wrapper,用于您不想直接公开的内部数组变量。我们尽量避免 classes 从数组或 IEnumerable 继承,因为有很多功能要实现,但为了添加额外功能而包装数组可能很有意义。
temps
在你的语法中 public float temps this[int index]
没有意义,也不是必需的,因为 class 只能有每个 type[ 的单个索引器=47=].
如果 temps
字段公开为 public,而不是使用索引器,那么您可以使用此语法来访问索引 5 的值:
TempratureRecord oTest = new TemperatureRecord();
var tempAtIndex5 = oTest.temps[5];
但现在您可以访问 temps
变量本身,这意味着您可以重新分配它。
Do not mistake that example as the only way to expose values from internal arrays, if you had multiple arrays then you can expose them in different ways, generally NOT by using an indexer.
如果索引器仅公开一个内部数组,但不提供任何其他内容,那么这可能根本不是使用它们的好理由。
你可以有一个 string
类型的索引器和一个单独的 int
类型的索引器,如果内部数组是一个可能同时具有唯一性的对象数组,这是一种常见的模式string 和 int 属性 可用于识别 return.
但是,您通常不会仅仅因为有一个整数数组和一个字符串数组就这样做。 Indexers 的一般期望是,如果有多个索引器,我们会为调用者提供一种不同的方式来访问相同的 conceptual 对象列表,并且实际上以不同的方式定位相同的对象,也许是因为我们 不知道 该对象在内部数组中的索引。
一个示例,说明如何使用多个索引器访问同一个内部数组,但显示传入的索引不必是内部使用的索引:
注意:通过这种方式,允许 setter 可能根本没有意义
public class Person
{
string Name { get; set; }
int Id { get; set; }
}
public class People
{
private Person[] _backingStore;
/// <summary>Instantiate a new list of people</summary>
public People(params Person[] persons)
{
_backingStore = persons;
}
/// <summary> Return a person from this list by Id </summary>
public Person this[int id]
{
get{
return _backingStore.Where(p => p.Id == id).FirstOrDefault();
}
}
/// <summary> Return a person from this list by Name </summary>
public Person this[string name]
{
get{
return _backingStore.Where(p => p.Name == name).FirstOrDefault();
}
}
}
...
People list = new People(
new Person(){ Id = 26, Name = "Hassan" },
new Person(){ Id = 101, Name = "John Skeet" }
);
var john = list["John Skeet"];
var alsoJohn = list[101];
var no1 = list["someone else"];
var hassan = list["Hassan"];