C#.Net 3.5使用具有不同返回类型的重载索引器
我有一个父类,它本质上是一个美化的列表。它由多个子类扩展以实现各种功能C#.Net 3.5使用具有不同返回类型的重载索引器,c#,.net-3.5,indexer,C#,.net 3.5,Indexer,我有一个父类,它本质上是一个美化的列表。它由多个子类扩展以实现各种功能 public class HierarchialItemList<ItemType> : IEnumerable<ItemType> { public ItemType this[String itemCode] { get { foreach (IHierarchialItem c
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);
}
}
}
为什么我们知道这是错误的?线路
Database testDatabase = sampleDatabaseList[0];
使用参数0
调用索引器,该参数是int
文本,因此,看到DatabaseList
继承自hieralilitemlist
将调用
public ItemType this[Int32 itemCode] { get; }
它被声明为返回一个ItemType
。您还没有告诉我们什么是ItemType
。我们没有理由知道ItemType
可以分配给数据库类型的变量
索引器不必返回相同的类型。但是,不可能仅根据返回类型来重载。就是这是合法的
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!";
}
}
}
回应您的编辑:
编辑:我刚刚发现这是因为使用枚举作为索引器,它在内部是一个整数。不过,有没有办法同时使用枚举和整数
如果要调用接受枚举的索引器,请按如下方式调用它:
sampleDatabaseList[Environments.RMSEnvironment.SomeEnumValue];
为什么我们知道这是错误的?线路
Database testDatabase = sampleDatabaseList[0];
使用参数0
调用索引器,该参数是int
文本,因此,看到DatabaseList
继承自hieralilitemlist
将调用
public ItemType this[Int32 itemCode] { get; }
它被声明为返回一个ItemType
。您还没有告诉我们什么是ItemType
。我们没有理由知道ItemType
可以分配给数据库类型的变量
索引器不必返回相同的类型。但是,不可能仅根据返回类型来重载。就是这是合法的
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!";
}
}
}
回应您的编辑:
编辑:我刚刚发现这是因为使用枚举作为索引器,它在内部是一个整数。不过,有没有办法同时使用枚举和整数
如果要调用接受枚举的索引器,请按如下方式调用它:
sampleDatabaseList[Environments.RMSEnvironment.SomeEnumValue];
在添加必要的类和属性以编译代码示例后,我能够毫无问题地运行此语句:
Database testDatabase=sampleDatabaseList[0]代码>
如果您遇到一个错误,sampleDatabaseList[0]
返回数据库列表,请提供一个可编译的代码示例,其中包含语句DatabaseList testDatabase=sampleDatabaseList[0]代码>在添加必要的类和属性以编译代码示例后,我能够毫无问题地运行此语句:
Database testDatabase=sampleDatabaseList[0]代码>
如果您遇到一个错误,sampleDatabaseList[0]
返回数据库列表,请提供一个可编译的代码示例,其中包含语句DatabaseList testDatabase=sampleDatabaseList[0]代码>这是完全有效的代码
class SomeClass { }
public class A<T> : IEnumerable<T>
{
public T this[int index]
{
get
{
return (this[index]);
}
}
public T this[String index]
{
get
{
return (this[index]);
}
}
}
public class B : A<SomeClass>
{
public B this[char typeToFilter]
{
get
{
return new B();
}
}
}
B classList = new B();
SomeClass test = classList[0];
class-SomeClass{}
公共A类:IEnumerable
{
公共T此[int索引]
{
得到
{
返回(此[索引]);
}
}
此[字符串索引]
{
得到
{
返回(此[索引]);
}
}
}
B级:A级
{
公共B此[字符类型过滤器]
{
得到
{
返回新的B();
}
}
}
B类列表=新的B();
SomeClass测试=类列表[0];
这是完全有效的代码
class SomeClass { }
public class A<T> : IEnumerable<T>
{
public T this[int index]
{
get
{
return (this[index]);
}
}
public T this[String index]
{
get
{
return (this[index]);
}
}
}
public class B : A<SomeClass>
{
public B this[char typeToFilter]
{
get
{
return new B();
}
}
}
B classList = new B();
SomeClass test = classList[0];
class-SomeClass{}
公共A类:IEnumerable
{
公共T此[int索引]
{
得到
{
返回(此[索引]);
}
}
此[字符串索引]
{
得到
{
返回(此[索引]);
}
}
}
B级:A级
{
公共B此[字符类型过滤器]
{
得到
{
返回新的B();
}
}
}
B类列表=新的B();
SomeClass测试=类列表[0];
---触发器--
创建触发器触发器测试
论电磁脉冲
插入、更新、删除后
作为
开始
声明@day varchar(10)
选择@day=datename(dw,getdate())
声明@hour int
选择@hour=convert(varchar(2),getdate(),114)
如果(@hour<9或@hour>13或@day='Saturday'或@day='Sunday')
开始
如果更新(EMPID)
RAISERROR('Error!',1,16)
回滚传输
结束
结束
插入EMP值(1003、'A'、'A')
跌落触发触发试验
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
命名空间TestNameSpace
{
公共类员工:人
{
字符串id;
公共字符串Id
{
获取{return id;}
设置{id=value;}
}
十进制工资;
公共十进制工资
{
获取{返回工资;}
设置{salary=value;}
}
字符串密码;
公共字符串密码
{
设置{password=value;}
}
int-ichk=1,schk=1,pchk=1;
公职人员()
:base()
{
Id=null;
工资=转换为特定值(“0.0”);
密码=null;
}
公共雇员(字符串n、字符g、日期时间d、字符串empid、十进制sal、字符串pwd)
:底座(n、g、d)
{
Id=empid;
薪金=sal;
密码=pwd;
}
公共覆盖无效接受()
{
base.Accept();
尝试
{
Console.Write(“输入EMPID:”);
Id=Console.ReadLine();
if(Id==null)
{
ichk=0;
Console.WriteLine(“未输入ID!”);
}
控制台。写入(“输入工资:”);
Salary=Convert.ToDecimal(Console.ReadLine());
如果(工资<0)
{