-4
returnTypeの値に基づいてこのメソッドの戻り値の型を変更するにはどうすればよいですか? int、float、およびdecimalsを返すために、それぞれに対して別々のメソッドを作成する必要はありません。具体的には、returnTypeの値に基づいて、最初の行の小数点をに変更するにはどうすればよいですか?引数の値に基づいてメソッドの戻り値の型を変更するにはどうすればよいですか?
public static decimal RequestNumber(string returnType)
{
Console.WriteLine(inputRequest);
decimal result;
switch(returnType)
case decimal:
bool parsed = decimal.TryParse(Console.ReadLine(), out result);
case int:
bool parsed = int.TryParse(Console.ReadLine(), out result);
case single:
bool parsed = single.TryParse(Console.ReadLine(), out result);
return result;
}
ご協力いただければ幸いです。
より良い..あなたが必要なのIntなると、入力パラメータとして小数に渡す 'ConvertTo'メソッドを使用するか、またはタイプを変更する方法を見始めます'C#Basics'を読み返すために戻る – MethodMan
重複しているように見えますが、入力パラメータでスピンを追加します:https://stackoverflow.com/questions/20705643/method-overloading-with-different-return-type – RetroCoder
Genericsメソッドを使用して、Tを返し、検出された各文字列タイプに対してFuncを返します。 –
RetroCoder