简体   繁体   English

对 C# 中的返回方法和空白感到困惑

[英]confused about return methods and voids in C#

Want feedback if i`m correct here?如果我在这里是正确的,想要反馈吗?

Use void if you are not returning anything in a method, otherwise如果您没有在方法中返回任何内容,请使用 void,否则

  1. Name your data types used in the method criteria before method name.在方法名称之前命名方法标准中使用的数据类型。
  2. use Return in the method before the calculation or output.在计算前的方法中使用 Return 或 output。

So something like this.所以像这样。

    static int MyMethod(int x) 
{
  return 5 + x;
}

static void Main(string[] args)
{
  Console.WriteLine(MyMethod(3));
}

// Outputs 8 (5 + 3)

What if my method has ints and doubles?如果我的方法有整数和双精度怎么办? Do I write as follows?我写如下吗? (another words do I have to mention every type i`m using prior to the method name? (换句话说,我必须在方法名称之前提及我正在使用的每种类型吗?

static int double myMethod (int x, double y) 

Even with that I dont know when is a method void?即使这样,我也不知道什么时候方法无效? It seems my methods all return values.看来我的方法都返回值。 Isnt the following returning the values of the arguments?以下不是返回 arguments 的值吗? So why should I label it void?那为什么我的label要作废呢?

static void MyMethod(string fname, int age) 
{
  Console.WriteLine(fname + " is " + age);
}

static void Main(string[] args)
{
  MyMethod("Liam", 20);
  MyMethod("Jenny", 25);
  MyMethod("Tom", 31);
}

I can only think that a void means there is no new calculation being done in the actual method body, passing arguments into a method and spitting them out for user viewing does not mean its "returning a value", I dont know what i`m talking about.我只能认为一个 void 意味着在实际方法体中没有进行新的计算,将 arguments 传递给一个方法并将它们吐出来供用户查看并不意味着它的“返回值”,我不知道我是什么谈论。

Let's be completely clear about what these bullets mean.让我们完全清楚这些项目符号的含义。

Use void if you are not returning anything in a method, otherwise如果您没有在方法中返回任何内容,请使用 void,否则

In this context, "return" means that the method provides an output that can be assigned to a variable by the caller.在这种情况下,“return”意味着该方法提供了一个 output 可以由调用者分配给一个变量。 For example例如

int Return10()
{
    return 10;
}

...allows the caller to do this: ...允许调用者这样做:

int x = Return10();
Console.WriteLine(x);  //Outputs "10"

A method should "return" void when its results cannot be assigned.当无法分配其结果时,方法应“返回” void。 For example, if the results are printed on the screen.例如,如果结果打印在屏幕上。

void Print10()
{
    Console.WriteLine("10"); //Prints 10 to the screen
}

...which allows the caller to do this: ...允许调用者这样做:

Print10();

You cannot assign it because it doesn't return anything.你不能分配它,因为它不返回任何东西。 This doesn't work:这不起作用:

int x = Print10(); //Compiler error

Name your data types used in the method criteria before method name.在方法名称之前命名方法标准中使用的数据类型。

A method can return exactly one value or object.一个方法可以只返回一个值或 object。 So "types" here is wrong.所以这里的“类型”是错误的。 You can only specify one type.您只能指定一种类型。

Use return in the method before the calculation or output.在计算前的方法中使用return或者output。

This is a little misleading.这有点误导。 The return keyword should be followed by an expression which can be assigned. return关键字后面应该跟一个可以赋值的表达式

int Return10()
{
    return 10 + 10; //Is okay because it's an expression and could be assigned
}

int Return10()
{
    var x = 10 + 10; 
    return x; //This is also okay; in fact it does exactly the same thing as the previous example
}

int Return10()
{
    return Console.WriteLine("10"); //Compiler error; can't be assigned to anything.
}

By the way, a method can also output something and return it:顺便说一句,一个方法也可以 output 东西并返回它:

int WriteAndReturn10()
{
    int x = 10;
    Console.WriteLine(x); 
    return x;
}

I am going to address the following我将解决以下问题

What if my method has ints and doubles?如果我的方法有整数和双精度怎么办? Do I write as follows?我写如下吗? (another words do I have to mention every type i`m using prior to the method name? (换句话说,我必须在方法名称之前提及我正在使用的每种类型吗?

There are no built in ways or syntax to return more than one type from a method as the return parameter.. This is basically historical and has been this way since dinosaurs roamed the earth.没有内置的方法或语法可以从一个方法返回多个类型作为返回参数。这基本上是历史性的,自从恐龙在地球上漫游以来一直如此。

However, there are lots of options that achieve the same result.但是,有很多选项可以达到相同的结果。 For instance, you could use a custom struct, you could use out parameters , you could use a class, or a delegate parameter of some kind.例如,您可以使用自定义结构,可以使用out parameters ,可以使用 class 或某种委托参数。 However, a modern succinct approach might be to use a Value Tuple :但是,现代简洁的方法可能是使用Value Tuple

static (int someInt, double someDouble) myMethod (int x, double y) 
{
    return (x,y);
}

Fun Fact : even though this looks like you a returning more than one type, you are actually just invoking a special syntax that wraps your return parameters in a single type of struct有趣的事实:尽管这看起来你返回了多个类型,但实际上你只是调用了一种特殊的语法,它将你的返回参数包装在一个单一类型的结构中

Usage用法

var result = myMethod(1,2.2);
Console.WriteLine(result.someInt);
Console.WriteLine(result.someDouble);

Or if you want to get fancy, you can use the newer deconstructed syntax或者如果你想变得花哨,你可以使用更新的解构语法

var (someInt, someDouble) = myMethod(1,2.2);
Console.WriteLine(someInt);
Console.WriteLine(someDouble);

Additional Resources其他资源

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

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