错误并非所有代码路径都返回值 [英] Error is not all code paths return a value
本文介绍了错误并非所有代码路径都返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Stud_Information为红色且此错误:
'Learning.Stud():'并非所有代码路径都返回值。
我用Stud方法调用学生课。但是这个错误。为什么要收到这个错误信息?
我试过的:
Student.cs
Stud_Information is red and this error :
'Students.Stud():' not all code paths return a value.
I call Students class in Stud method.But take this error.Why take this error message?
What I have tried:
Student.cs
namespace WorkList
{
public class Stud_Information
{
public List<string> students= new List<string>();
public static string Stud()
{
List<string> students = new List<string>();
students.Add("Mary");
}
}
}
计划.cs
Program.cs
namespace WorkList
{
class Program
{
static void Main(string[] args)
{
Stud_Information.Stud();
for (var i = 0; i < students.Count; i++)
{
Console.WriteLine("[" + i.ToString() + "] " + students[i] );
}
Console.ReadKey();
}
}
推荐答案
你的Stud()
方法被定义为返回一个字符串
的值,你没有返回。
要么返回方法中的字符串值:返回Mary;
或者将方法定义更改为return void:public static void Stud()
YourStud()
method is defined as returning astring
value which you are not returning.
Either return a string value in the method :return "Mary";
Or change the method definition to return void :public static void Stud()
这篇关于错误并非所有代码路径都返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文