所以基本上我剛剛開始學習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'表達式,如果你創建另一種語言,如果你有一天不是新手? –