C# this索引与Item属性冲突
public interface In_ModelBase
{
System.Collections.Generic.Dictionary<string, object> Item
{
get;
}
object this[string name]
{
get;
}
}
以上代码在编译的时候报出错误 类型“E.Interface.Model.In_ModelBase”已经包含“Item”的定义 求高人详细解释是为什么。
难道是因为定义this索引,于是自动产生了Item属性?如果是,请给出有力的参考资料
答案:这个问题是为了兼容VB.net和C#。在MSDN有这样一段话:C# 语言使用 this 关键字来定义索引器而不是通过实现 Item 属性。 Visual Basic 将 Item 实现为默认属性,该属性提供相同的索引功能(参考, http://msdn.microsoft.com/zh-cn/library/system.collections.arraylist.item.aspx#Y970,点击)
在VB.net中,通过Item代替C#中的索引器。比如我用反编译工具查看List(Of T) 代码:
Public Property Item As T
Get
If (index >= Me._size) Then
ThrowHelper.ThrowArgumentOutOfRangeException()
End If
Return Me._items(index)
End Get
Set(ByVal value As T)
If (index >= Me._size) Then
ThrowHelper.ThrowArgumentOutOfRangeException()
End If
Me._items(index) = value
Me._version = Me._version + 1
End Set
End Property
【结论:Item已经是VB.net的关键字,等同于C#中索引器功效】
上一个:怎样用C#在控制台输出Windows命令的帮助信息
下一个:C#16进制转换成10进制