并非所有的路径代码都有返回值

yoyoyuye 2009-11-06 10:51:56

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TestIndex
{
public class SpellString
{
protected string[] theString=new string[10];
public SpellString()
{
for (int i = 0; i < 10; i++)
{


Console.WriteLine(theString[i]);
}

}
public string this[int index]
{
get
{

if (index < 0 || index >= 10)
{

//错误处理
}
else
return theString[index];

}
set
{

if (index < 0 || index >= 10)
{

//错误处理
}
else
theString[index] = value;
}

}


}
public class Program
{
static void Main(string[] args)
{
SpellString thisString = new SpellString();
thisString[2] = "HEllo";
thisString[3] = "world";
for (int i=0;i<10;i++)
{

Console.WriteLine("value:{0}={1}",i,thisString[i]);
}
}
}
}



提示get行错误。并非所有代码都有返回值
...全文
53 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
yoyoyuye 2009-11-06
  • 打赏
  • 举报
回复
楼上兄弟,刚好结贴了。
wqzone 2009-11-06
  • 打赏
  • 举报
回复
Get 必须要有返回值,实在没有就 return null ;
ctwei86 2009-11-06
  • 打赏
  • 举报
回复
两个办法,最常用的是在你那if里面return 一个值,同2楼,哪怕返回一个空值都可以。
还有个办法是可以用throw抛一个异常。
yoyoyuye 2009-11-06
  • 打赏
  • 举报
回复
谢谢
nashina 2009-11-06
  • 打赏
  • 举报
回复
你在if里面写个返回,哪怕返回一个空值
ziminyang 2009-11-06
  • 打赏
  • 举报
回复
是啊,get必须要返回指定的类型的,而你的那个if里什么都没有啊

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧