对于IList< T>的IndexOutOfRangeException [英] IndexOutOfRangeException for an IList<T>

查看:109
本文介绍了对于IList< T>的IndexOutOfRangeException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在执行一个列表,我想知道IndexOutOfRange的定义。你认为哪一个更好?

  ///< exception cref =IndexOutOfRangeException> if index小于0 
///或大于< see cref =Count/>
public T this [int index] {get {return myArray [index];






$ b $ p $ ///< exception cref =IndexOutOfRangeException>如果索引超出有效范围
///等于< see cref =Count/> < /异常>
public T this [int index] {get {return myArray [index]; }}

我正在考虑这种情况,当这个类被从使用.NET语言的索引数组从1开始我不太了解这个主题,但是比第一个版本要好的第二个版本?

解决方案

稍微OT:可以封装一个列表而不是一个数组,然后一切都会清空,因为封装的列表会产生适当的异常?



编辑:如果您真的需要内部的数组,那么将包装在访问器首个中的列表中,然后按索引进行选择。这样,从列表索引到数组索引的翻译就会发生在您的组件的语言之内,而不是调用者。



大概是列表索引的语义不会改变.Net语言?这是您的组件应该遵循的,因为它实现了列表界面。



再次编辑实际上,这是一个问题人们通过访问器访问您的阵列,该访问器使用您所控制的语言编写,因此知道数组索引从哪里开始。即使你从VB.Net调用者调用你的(说)C#类,访问者仍然会使用C#的数组索引的想法,不是吗?


I am implementing a list, and I am wondering about the definition of the IndexOutOfRange. Which one of the following do you think is better?

/// <exception cref="IndexOutOfRangeException">if index is less than 0
/// or greater than <see cref="Count"/>
public T this[int index] { get { return myArray[index]; } }

Or

/// <exception cref="IndexOutOfRangeException">if index outside the valid range
/// for an array of length equal to <see cref="Count"/></exception>
public T this[int index] { get { return myArray[index]; } }

I am thinking about the case when this class would be used from a .NET language that indexes arrays starting from 1. I don't know much about the topic, but is the second version better than the first by any means?

解决方案

Slightly OT: can you encapsulate a list rather than an array, and then everything falls out in the wash since the encapsulated list will generate appropriate exceptions?

Edit: if you really need the array internally, how about wrapping it in a list inside the accessor first, and then selecting by index. This way the translation from list index to array index occurs inside the language of your component rather than the caller's.

Presumably the semantics of list indexing doesn't change across .Net languages? This is what your component should follow since it implements the list interface.

Edit again: actually, is this a problem at all??

People access your array through the accessor, which is written in a language that you control and so know where array indexing starts from. Even if you call your (say) C# class from a VB.Net caller, the accessor will still use C#'s idea of array indexes, won't it?

这篇关于对于IList&lt; T&gt;的IndexOutOfRangeException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆