简体   繁体   English

方法的返回类型的目的

[英]Purpose of return types for methods

I am having issues understanding the syntax of C# Methods - specifically the "must return something" error. 我在理解C#方法的语法时遇到问题-特别是"must return something"错误。

I have this method: 我有这种方法:

public static class Connection
{
    public static List<string> getClients()
    {   
        List<string> clients = new List<string>();
        return clients;
    }
}

The method is incorrect since I get invalid expression term "return" , so I don't exactly know what to do. 该方法不正确,因为我得到了invalid expression term "return" ,所以我不知道该怎么做。 Can someone explain me how this public void, etc, works? 有人可以向我解释这个公共空白等如何工作吗?

Also, why I cant do the following? 另外,为什么我不能做到以下几点?

public getClients()
    {   
        List<string> clients = new List<string>();
        return clients;
    }

I get an error saying method must have a return type 我收到一条错误消息,说method must have a return type

Every method needs to have a return type, or be declared with a void return type. 每个方法都需要具有返回类型,或使用无效的返回类型声明。

If you don't want to return anything, you would have a void return type, like below... 如果您不希望返回任何内容,则将有一个无效的返回类型,如下所示...

public void printSomething(string something)
{
  System.out.println(something);
}

If you want to return something, you have to declare the return type, like below... 如果要返回某些内容,则必须声明返回类型,如下所示...

public string returnSomething()
{
  string something = "something";
  return something;
}

So for your example, if you are returning " clients " which is of type List<string> , then you need to declare that return type, like below... 因此,以您的示例为例,如果您要返回类型为List<string>clients ”,则需要声明该返回类型,如下所示...

public List<string> getClients()
{
  List<string> clients = new List<string>();
  return clients;
}

What you're working with are called return types . 您正在使用的东西称为返回类型 When you define a method, you can set a type (such as integer, Bitmap, string, or even a custom class) to be returned as a value. 定义方法时,可以设置要作为值返回的类型(例如整数,位图,字符串,甚至是自定义类)。 That way, you can do the following: 这样,您可以执行以下操作:

int number = Average();

For a method that does not return a value, you can set the void keyword to signify that this method performs actions, but does not get a tangible result. 对于返回值的方法,可以设置void关键字以表示该方法执行了操作,但没有得到明显的结果。 Whereas the Average() method above returns an int when it's finished, a void returns nothing. 上面的Average()方法完成后会返回一个int,而void则什么也不会返回。

Additionally, public and static are keywords are adjectives that describe your method, be it void or not. 此外, publicstatic是关键字,它们是描述您的方法的形容词,无论它是否无效。 Public refers to its privacy (or what sections of code can use it) and static refers to the lifetime and reference of a method. 公共是指其隐私(或代码的哪些部分可以使用它),而静态是指方法的生存期和引用。

The first piece of code should work. 第一部分代码应该工作。

If you don't want to return anything from a function, make it return void: 如果您不想从函数返回任何内容,请使其返回void:

public void DoWork() {
    int i = 1 + 1;
} // Don't return anything.

You are getting error saying method must have a return type in the second code because you are not returning a list but the return type is not present in the method signature. 您收到错误消息,指出method must have a return type在第二个代码中method must have a return type ,因为您没有返回列表,但方法签名中不存在返回类型。 For a method with no return type you should use void . 对于没有返回类型的方法,应使用void

public void method()//void or no return type
{
   //do something
}

Your first code is absolutely fine and should work without error 您的第一个代码绝对正确,应该可以正常运行

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM