如何重构方法

我有两种方法。它们非常相似。我试过使用泛型,但它不适用于 TryParse()


    public static int EnterIntengerNumber()

    {

        while (true)

        {

            Console.Write("Enter an intenger number: ");

            if (int.TryParse(Console.ReadLine(), out int number))

            {

                return number;

            }

            else

            {

                ConsoleError("Incorrect value");

            }

        }

    }



    public static double EnterRealNumber()

    {

        while (true)

        {                

            Console.Write("Enter a number: ");

            if (double.TryParse(Console.ReadLine(), out double number))

            {

                return number;                    

            }

            else

            {

                ConsoleError("Incorrect value");

            }

        }

    }

我如何组合或重构它们?


叮当猫咪
浏览 82回答 3
3回答

哈士奇WWW

您可以创建一个函数,它接受一个带有TryParse参数和返回值的委托作为通用的。你也可以/将不得不制作字符串来询问正确的类型。但这可以从这个例子中得到改进:&nbsp; &nbsp; static void Main(string[] args)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; var myDouble = EnterSomething<double>("a double", double.TryParse);&nbsp; &nbsp; &nbsp; &nbsp; var myInt = EnterSomething<int>("an interger", int.TryParse);&nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine(myDouble);&nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine(myInt);&nbsp; &nbsp; &nbsp; &nbsp; Console.ReadLine();&nbsp; &nbsp; }&nbsp; &nbsp; public delegate bool TryParseFunction<Tout>(string input, out Tout output);&nbsp; &nbsp; public static Tout EnterSomething<Tout>(string typeName, TryParseFunction<Tout> parser)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; while (true)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.Write("Enter " + typeName + " number: ");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (parser(Console.ReadLine(), out Tout number))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return number;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine("Incorrect value");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }小的额外解释:int.TryParse传递给 EnterSomething 的不是调用 TryParse。它实际上只是“传递”函数,以便可以在 EnterSomething 中调用它。不使用的原因Func<>是它不支持输出变量,请看这里的一些例子:Func<T> with out parameter编辑:我删除了 Tin 类型参数,因为无论如何输入都是字符串。

阿晨1998

你可以这样试试:public static int EnterIntengerNumber(){&nbsp; &nbsp; return EnterNumber(t => int.TryParse(t, out int v) ? (int?)v : null)}public static double EnterRealNumber(){&nbsp; &nbsp; return EnterNumber(t => double.TryParse(t, out double v) ? (double?)v : null)}private static T EnterNumber<T>(Func<string, T?> tryParse) where T : struct{&nbsp; &nbsp; while (true)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; Console.Write("Enter a number: ");&nbsp; &nbsp; &nbsp; &nbsp; T? result = tryParse(Console.ReadLine());&nbsp; &nbsp; &nbsp; &nbsp; if (result.HasValue)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return result.Value;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ConsoleError("Incorrect value");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}

回首忆惘然

您可以使用Convert.ChangeType请记住,您最终将使用装箱,因此失去了使用泛型的意义。尽管您最终确实拥有更少的代码。&nbsp; &nbsp; public static T EnterNumber<T> ()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; while (true)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.Write("Enter an " + typeof(T) + " number: ");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return (T) Convert.ChangeType(Console.ReadLine(), typeof(T));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; catch (FormatException ex)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Console.Write("Incorrect format" + ex.Message);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP