我在理解C#方法的语法时遇到了问题 - 特别是"must return something"
错误.
我有这个方法:
public static class Connection
{
public static List<string> getClients()
{
List<string> clients = new List<string>();
return clients;
}
}
Run Code Online (Sandbox Code Playgroud)
因为我得到了这个方法不正确invalid expression term "return"
,所以我不知道该怎么做.有人可以解释一下这个公共空白等是如何起作用的吗?
另外,为什么我不能做以下事情?
public getClients()
{
List<string> clients = new List<string>();
return clients;
}
Run Code Online (Sandbox Code Playgroud)
我收到一个错误说 method must have a return type
每个方法都需要具有返回类型,或者使用void返回类型声明.
如果你不想返回任何东西,你将有一个void返回类型,如下所示......
public void printSomething(string something)
{
System.out.println(something);
}
Run Code Online (Sandbox Code Playgroud)
如果你想返回一些东西,你必须声明返回类型,如下所示......
public string returnSomething()
{
string something = "something";
return something;
}
Run Code Online (Sandbox Code Playgroud)
因此,对于您的示例,如果您返回clients
类型的" " List<string>
,那么您需要声明返回类型,如下所示...
public List<string> getClients()
{
List<string> clients = new List<string>();
return clients;
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
272 次 |
最近记录: |