2017-04-14 29 views
0
public static int getInfo(string info) 
    { 
     string inputValue; 
     int infor; 
     Console.WriteLine("Information of the employee: {0}", info); 
     inputValue = Console.ReadLine(); 
     infor = int.Parse(inputValue); 
     return infor; 

    } 

在上面的代碼中,我如何獲得一個人的名稱(字符串)和工資(int)?規範是我需要調用該方法兩次的信息檢索。從相同的方法返回字符串和int

+0

所以你想你的方法返回一個字符串或一個int? –

+2

使用C#7 +元組:https://blogs.msdn.microsoft.com/dotnet/2016/08/24/whats-new-in-csharp-7-0/#user-content-tuples。 –

+1

這個尖叫XY問題(如果你不知道這意味着什麼,Google它,我沒有手機上的鏈接)。你想用這個做什麼? –

回答

5

如果創建持有信息的類和工資

public static int getInfo(string info,int salary) 

或創建一個類可以單獨或更好的傳遞,

public class MyInfo 
    { 
     public int info { get; set; } 
     public int salary { get; set; } 

    } 

和方法簽名去的,

public static int getInfo(MyInfo info) 

如果要同時返回字符串和整型,最好將方法簽名更改爲類型類MyInfo

public static MyInfo getInfo(MyInfo info) 
+1

我認爲OP希望他的方法能夠返回信息。 –

+0

@PieterWitvoet ok編輯原樣。另外2個用c#獲取金幣! :) – Sajeetharan

+0

@Sajeetharan 2更多黃金!妒!祝你好運,你會快速得到它 – EpicKip

1

你可以把它返回一個元組,像這樣兩個值:

internal Tuple<int, string> GetBoth(string info) 
{ 
    string inputValue; 
    int infor; 
    Console.WriteLine("Information of the employee: {0}", info); 
    inputValue = Console.ReadLine(); 
    infor = int.Parse(inputValue); 
    return new Tuple<int, string>(infor, inputValue); 
} 

internal void MethodImCallingItFrom() 
{ 
    var result = GetBoth("something"); 
    int theInt = result.Item1; 
    string theString = result.Item2; 
} 
-1

你爲什麼不只是返回一個字符串數組的大小爲2?在第一(數組[0])有串,並在第二(數組[0])有整型...

public static string[] GetInfo (string info) 

我希望我理解你的問題正確的^^

+0

我本來可以用TryParse來做到這一點,如果其他情況,但情況是,我需要調用該方法兩次。 :( –

+0

你也可以使'string stringname + Convert.ToString(int intname);' –

-1

你需要創建一個Person類和閱讀的名字和工資

public class Person 
{ 
    public string Name {get; set;} 
    public decimal Salary {get; set;} 
} 

,你的功能將是:

public static Person getInfo(string info) 
{ 
    string inputName; 
    string inputSalary; 

    Console.WriteLine("Information of the employee: {0}", info); 

    Console.WriteLine("Name:"); 
    inputName = Console.ReadLine(); 

    Console.WriteLine("Salary:"); 
    inputSalary = Console.ReadLine(); 

    Person person = new Person(); 
    person.Name = inputName; 
    person.Salary = int.Parse(inputSalary); 
    return person; 

} 
-1

如果你想要一個實現方法具d返回不同的信息類型,然後我會使用泛型:

public static T GetInfo<T>(string name); 

// This can be called as following: 
string name = GetInfo<string>("name"); 
int salary = GetInfo<int>("salary"); 

還有一個問題,雖然:Console.ReadLine返回string,而我們的方法可以返回任何類型。它如何將字符串轉換爲其'目標'類型?您可以檢查T併爲您想要支持的所有類型編寫自定義邏輯,但這很麻煩而且很脆弱。更好的方法是讓調用者傳遞一個知道如何將字符串轉換成特定類型的一個小功能:

public static T GetInfo<T>(string name, Func<string, T> convert); 

// This can be called as following: 
string name = GetInfo<string>("name", s => s); 
int salary = GetInfo<int>("salary", int.Parse); 

現在,你如何實現一個方法是什麼?

public static T GetInfo<T>(string name, Func<string, T> convert) 
{ 
    Console.WriteLine("Please enter " + name); 
    string input = Console.ReadLine(); 
    return convert(input); 
} 

的幾個注意事項:

  • 類型參數往往只是命名爲T,但我覺得它非常有用,給他們更多的描述性的名稱,如TInfo
  • <string><int>部分可以在第二個例子中省略,因爲編譯器有足夠的信息來推斷它們的類型。
  • 我已經離開了我們的錯誤處理,以保持示例簡潔。但是,在生產代碼中,您會想出一個處理無效輸入的策略。
0
public static void Main(string[] args) 
    { 
     string eName, totalSales; 
     double gPay, tots, fed, sec, ret, tdec,thome; 
     instructions(); 
     eName = getInfo("Name"); 
     totalSales = getInfo("TotalSales"); 
     tots = double.Parse(totalSales); 
     gPay = totalGpay(tots); 

}

public static string getInfo(string info) 
    { 
     string inputValue; 
     Console.WriteLine("Information of the employee: {0}", info); 
     inputValue = Console.ReadLine(); 

     return inputValue; 

    } 

這是需要什麼。可以用你們提到的其他技巧來完成。無論如何謝謝你。