gpt4 book ai didi

C# .Net 3.5 使用具有不同返回类型的重载索引器

转载 作者:太空宇宙 更新时间:2023-11-03 18:48:31 24 4
gpt4 key购买 nike

我有一个父类,它本质上是一个美化列表。它由多个子类扩展以实现各种功能。

public class HierarchialItemList<ItemType> : IEnumerable<ItemType>
{
public ItemType this[String itemCode]
{
get
{
foreach (IHierarchialItem curItem in hierarchialItems)
{
if (curItem.Code.Equals(itemCode, StringComparison.CurrentCultureIgnoreCase))
{
return ((ItemType)curItem);
}
}
return (default(ItemType));
}
}
public ItemType this[Int32 index]
{
get
{
return (hierarchialItems[index]);
}
}
}

public class DatabaseList : HierarchialItemList<Database>
{
public DatabaseList this[CommonDatabaseType typeToFilter]
{
get
{
DatabaseList returnList = new DatabaseList();
foreach(Database curDatabase in this)
{
if (curDatabase.DatabaseType.Equals(typeToFilter))
{
returnList.Add(curDatabase);
}
}
return (returnList);
}
}

public DatabaseList this[Environments.RMSEnvironment environmentToFilter]
{
get
{
DatabaseList returnList = new DatabaseList();
foreach(Database curDatabase in this)
{
if (curDatabase.ParentEnvironment.Equals(environmentToFilter))
{
returnList.Add(curDatabase);
}
}
return (returnList);
}
}


}

问题是 C# 认为:

数据库 testDatabase = sampleDatabaseList[0];

是一个错误,索引器应该返回一个 DatabaseList,而不是一个数据库。你我都知道那是假的。是否有任何解决方法或所有索引器都必须具有相同的返回类型?

编辑:我刚刚发现这是因为使用枚举作为内部整数的索引器。还有,有什么方法可以同时使用枚举和整数吗?

编辑 2:根据要求,这里有一些可编译的测试代码来演示问题。

using System;
using System.Collections.Generic;

namespace CSQT
{
class A<T>
{
List<T> temp;

public A()
{
temp = new List<T>();
}

public void AddItem(T itemToAdd)
{
temp.Add(itemToAdd);
}

public T this[String code]
{
get { return (temp[0]); }

}

public T this[Int32 index]
{
get { return (temp[index]); }

}
}

class B : A<String>
{
public B()
: base()
{
}

public B this[BTypes testType]
{
get
{
return (this);
}
}
}

enum BTypes { TEMP1, TEMP2 };

class C
{
public C()
{
B temp = new B();

//Compile error: Cannot implicitly convert type 'CSQT.B' to 'string'
String temp2 = temp[0];

//Compile error: Cannot convert type 'CSQT.B' to 'string'
String temp3 = (String)temp[0];

//This compiles and runs but instead of going to A.this[int32], it goes to B.this[BTypes testType]
B temp4 = temp[0];
}
}
}

最佳答案

为什么我们知道那是假的?线路

Database testDatabase = sampleDatabaseList[0];

使用参数 0 调用索引器这是 int字面意思,因此,看到 DatabaseList继承自 HierarchialItemList<Database>将调用定义的索引器

public ItemType this[Int32 itemCode] { get; }

声明返回 ItemType .你还没告诉我们什么ItemType是。我们没有理由知道 ItemType可以分配给类型为 Database 的变量.

索引器不必返回相同的类型。但是,不可能仅根据返回类型进行重载。也就是说,这是合法的

class IndexerTest {
public int this[int index] {
get {
return 0;
}
}

public string this[double index] {
get {
return "Hello, success!";
}
}
}

这不是

class IndexerTest {
public int this[int index] {
get {
return 0;
}
}

public string this[int index] {
get {
return "Hello, fail!";
}
}
}

响应您的编辑:

Edit: I just figured out that it's because of using an enumeration as an indexer which is internally an integer. Still, any way to use both an enumeration and an integer?

如果你想调用接受枚举的索引器,像这样调用它:

sampleDatabaseList[Environments.RMSEnvironment.SomeEnumValue];

关于C# .Net 3.5 使用具有不同返回类型的重载索引器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2135776/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com