积极答复者
C# 的return0;在什么情况下要被使用,表示什么意思呢

问题
答案
全部回复
-
class IndexerClass
{
private int[] arr = new int[100];
public int this[int index] // Indexer declaration
{
get
{
// Check the index limits.
if (index < 0 || index >= 100)
{
return 0;
}
else
{
return arr[index];
}
}
set
{
if (!(index < 0 || index >= 100))
{
arr[index] = value;
}
}
}
}class MainClass
{
static void Main()
{
IndexerClass test = new IndexerClass();
// Call the indexer to initialize the elements #3 and #5.
test[3] = -2;
test[5] = 120;
for (int i = 0; i <= 10; i++)
{
System.Console.WriteLine("Element #{0} = {1}", i, test[i]);
Console.ReadKey();
}
}
}
}为什么这段程序里的return 0;它其中有两个返回的确是实际的数test[3]=-2;test[5]=120,这种情况下这两个数都越界了为什么还要让它们返回0表示成功呢,为什么不抛出异常,请问是从什么角度来考虑这个问题的呢,是因为当计算索引器的访问时(例如,在 Console.Write 语句中),将调用 get 访问器。因此,如果 get 访问器不存在,将发生编译时错误这个原因吗,那岂不是如果有越界的数都要被输出来吗,对程序不是很不利吗,请指教谢谢
-
有关 Main 函数返回值的问题,请参考我的文章:C# tutorials (6): The Main method
Mark Zhou