简体   繁体   English

关于使用委托进行解析,我在 C# 中做得对吗?

[英]Am I doing it right C# regarding parsing using delegate?

I want to parse string into version numbers using delegate.我想使用委托将字符串解析为版本号。 The delegate requires a string as an argument and produces integer array as an output.委托需要一个字符串作为参数,并生成整数数组作为输出。

There are two errors有两个错误

Error CS0136 A local or parameter named ' arrayString ' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter错误 CS0136 无法在此范围内声明名为“ arrayString ”的本地或参数,因为该名称在封闭的本地范围中用于定义本地或参数

Error CS0029 Cannot implicitly convert type ' System.Collections.Generic.List<int> ' to ' int '错误 CS0029 无法将类型“ System.Collections.Generic.List<int> ”隐式转换为“ int

namespace TEST
{
    class Program
    {
        public delegate int Parsing(string parsee);
       static void Main(string[] args)
        {

            Parsing parsee = new Parsing(Parse);
            Console.WriteLine(); 
        }

        public static int Parse(string arrayString)
        {
            Console.WriteLine("Write the version numbers : ");
            var input = Console.ReadLine();
             string[] arrayString = input.Split('-');
            List<int> listInt = new List<int>();
            foreach (string i in arrayString)
            {
                listInt.Add(Convert.ToInt32(i));
            }
            listInt.ToArray();
            foreach (var item in listInt)
            {
                Console.WriteLine(item);
            }

            return listInt;
        }
    }
}

I am a noob.我是菜鸟。

The identifier "arrayString" is defined twice.标识符“arrayString”被定义了两次。

Local variables as well as arguments are not allowed to be defined with the same name, otherwise accessing the identifier would have ambiguous meaning.局部变量和参数不允许使用相同的名称定义,否则访问标识符将具有不明确的含义。

As long as the Parse method is named "parse", it should act as-is.只要Parse方法被命名为“parse”,它就应该按原样运行。 It takes an argument as input and outputs the produced result, which is the only duty of it.它接受一个参数作为输入并输出产生的结果,这是它的唯一职责。 while Console.ReadLine or Console.WriteLine are not a part of "Parse" but "Input".Console.ReadLineConsole.WriteLine不是“解析”而是“输入”的一部分。

You need more learning on the basic syntax and the built-in types before you start learning delegate.在开始学习委托之前,您需要更多地了解基本语法和内置类型。

The solution that maybe matches your requirement as a demo is:作为演示,可能符合您要求的解决方案是:

namespace Test
{
    internal static class Program
    {
        internal static void Main(string[] args)
        {
            Console.WriteLine("Write the version numbers : ");
            var input = Console.ReadLine();

            var results = ParseInts(input.Split('-'), ParseInt);
            foreach (var item in results)
            {
                Console.WriteLine(item);
            }
        }

        private static IEnumerable<int> ParseInts(IEnumerable<string> values, Func<string, int> parser)
        {
            foreach (var item in values)
            {
                yield return parser(item);
            }
        }

        private static int ParseInt(string value)
        => Convert.ToInt32(value);
    }
}

But simply we use this to get int values:但我们简单地使用它来获取 int 值:

var input = Console.ReadLine();
var results = input.Split('-').Select(int.Parse);

instead of writing everything.而不是写一切。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM