所以基本上我刚刚开始学习C#,昨天我有机会了解方法,我有点困惑如何返回有用或他们是什么,我知道你不能有一个返回一个具有void作为关键字的一部分的方法,我知道void表示“不做任何事或不返回任何东西”类似的东西。C#方法,返回
那么知道void就意味着。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication6
{
class Program
{
static void Main(string[] args)
{
Add(2,2);
}
static int Add(int numberOne, int numberTwo)
{
Console.WriteLine(numberOne + numberTwo);
return numberOne + numberTwo;
}
}
}
如何是在此代码“返回的Numberone + numberTwo”有用的,我有点卡住了,我有心理障碍,为什么我需要的回报。 当我运行这段代码我得到的东西(4)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.IO;
using System.Net;
namespace MethodsTutorial
{
public class Program
{
static void Main(string[] args)
{
Console.WriteLine("I'm about to go into a method.");
DoSomethingAwesome();
Console.WriteLine("I'm back from the method.");
}
static void DoSomethingAwesome()
{
Console.WriteLine("I'm inside of a method, doing something awesome!");
}
}
}
为什么在这段代码我也有东西(“我是一个方法里面,做一些真棒!”),我不使用关键字返回在最后,我不知道我是否足够好地解释自己(对不起,如果我不英语不是我的第一语言),但我只需要一个基本的和很好的解释如何返回有用,它们是什么,什么时候使用它们,何时不使用它们。
谢谢...
IMO,我基本上使用返回类型的方法,当我想修改我的方法的上下文中的东西,然后只是返回输出,当我想设置可在其他地方访问的东西(全局变量,等等)时,Void不能使用@oneWhisper – Barak
@oneWhisper。 ** ** Ed Hastings **(http://softwareengineering.stackexchange.com/questions/180804/what-is-the-reason-behind-methods-with-return-values-and-methods-with-void)将如果你阅读有帮助。 –
也许你可以省略'return'表达式,如果你创建另一种语言,如果你有一天不是新手? –