本文介绍了错误并非所有代码路径都返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
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() Your Stud() method is defined as returning a string 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()
更多推荐
错误并非所有代码路径都返回值
发布评论