C# - Error quot;not all code paths return a valuequot; with an array as out parameter(C# - 错误“并非所有代码路径都返回值;以数组作为输出参数)
问题描述
我目前有以下代码:
public int GetSeatInfoString(DisplayOptions choice, out string[] strSeatInfoStrings)
public int GetSeatInfoString(DisplayOptions choice, out string[] strSeatInfoStrings)
{
strSeatInfoStrings = null;
int count = GetNumOfSeats(choice);
if ((count <= 0))
return 0;
strSeatInfoStrings = new string[count];
int i = 0;
for (int index = 0; index <= m_totNumOfSeats - 1; index++)
{
if (string.IsNullOrEmpty(m_nameList[index]))
strSeatInfoStrings[i++] =
m_nameList[index].ToString();}
m_nameList[index].ToString(); }
}
此代码产生错误...GetSeatInfoString.DisplayOptions, out string[])":并非所有代码路径都返回一个值.基本上,我希望在上述方法中执行的操作是循环一个数组和数组中包含字符串的任何值,我希望将它们添加到新数组中,strSeatInfoStrings 反过来可以从单独的类中调用,然后新的数组内容显示在列表框中.
This code produces an error of, "...GetSeatInfoString.DisplayOptions, out string[])': not all code paths return a value. Basically, what I am looking to do in the above method is to cycle through an array and for any values in the array that contain a string, I want these then adding to the new array, strSeatInfoStrings which in turn, can be called from a separate class and the new array content then displayed in a listbox.
关于如何纠正此问题的任何建议?
Any suggestions on how to rectify this?
提前致谢
推荐答案
可以在末尾加上 return strSeatInfoStrings.Length
You can add return strSeatInfoStrings.Length
at the end
public int GetSeatInfoString(DisplayOptions choice, out string[] strSeatInfoStrings)
{
strSeatInfoStrings = null;
int count = GetNumOfSeats(choice);
if ((count <= 0))
return 0;
strSeatInfoStrings = new string[count];
int i = 0;
for (int index = 0; index <= m_totNumOfSeats - 1; index++)
{
if (string.IsNullOrEmpty(m_nameList[index]))
strSeatInfoStrings[i++] =
m_nameList[index].ToString(); }
return strSeatInfoStrings.Length;
}
这篇关于C# - 错误“并非所有代码路径都返回值";以数组作为输出参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:C# - 错误“并非所有代码路径都返回值";以数组作为输出参数


- 输入按键事件处理程序 2022-01-01
- WebMatrix WebSecurity PasswordSalt 2022-01-01
- 在哪里可以找到使用中的C#/XML文档注释的好例子? 2022-01-01
- MoreLinq maxBy vs LINQ max + where 2022-01-01
- C# 中多线程网络服务器的模式 2022-01-01
- 如何用自己压缩一个 IEnumerable 2022-01-01
- C#MongoDB使用Builders查找派生对象 2022-09-04
- 良好实践:如何重用 .csproj 和 .sln 文件来为 CI 创建 2022-01-01
- Web Api 中的 Swagger .netcore 3.1,使用 swagger UI 设置日期时间格式 2022-01-01
- 带有服务/守护程序应用程序的 Microsoft Graph CSharp SDK 和 OneDrive for Business - 配额方面返回 null 2022-01-01