【问题标题】:Initializing new class objects from Console.Readline() and adding them into list从 Console.Readline() 初始化新的类对象并将它们添加到列表中
【发布时间】:2021-06-02 21:54:18
【问题描述】:

我遇到了一个 3 天以来我无法解决的问题。我将非常感谢任何建议。

我希望用户通过 Console.Readline() 键入构造函数中引用的所有属性来初始化对象,然后该对象应存储在对象列表中。

namespace ExampleA {

    public class ExampleClass 
    {

        public string Name {get; set;}
        public int Age {get; set;}

        public Example(string name, int age) {
        this.Name = name;
        this.Age = age; 
        }
    }

    class Program {

    static void Main(string[] args) 
    {

        List<ExampleClass> example_list = new List<ExampleClass>();

        example_list.Add(new ExampleClass
        {
          name = Console.Readline();
          age = Convert.ToInt32(Console.Readline());
        }
    }

每次尝试编译后都会收到错误消息:没有给出与“ExampleClass.Example(string, int)”所需的形参“name”相对应的参数。

【问题讨论】:

  • 理解异常消息属于基本的编程技能。您不必在 Stack Overflow 上询问它们的含义。

标签: c# list visual-studio class oop


【解决方案1】:

@Tim Schmelter 指出的一个错误是您输入了错误的属性名称:它们应该是 NameAge。你应该写Console.ReadLine()而不是Console.Readline()

另外,您声明了一个需要 name 和 age 作为参数的构造函数,它是对象的唯一构造函数,因此您必须在使用语法 new ExampleClass(name, age) { //setting other properties } 创建实例时使用它 提供一个空构造函数。这是一个例子:

public class ExampleClass
{

    public string Name { get; set; }
    public string Surname { get; set; }
    public int Age { get; set; }


    public ExampleClass(string name, int age)
    {
        Name = name;
        Age = age;
    }

}

public class Program
{

    static void Main(string[] args)
    {

        List<ExampleClass> example_list = new List<ExampleClass>();

        example_list.Add(new ExampleClass(Console.ReadLine(), Convert.ToInt32(Console.ReadLine()))
        {
            Surname = Console.ReadLine()
        });

        //The following does not compile
        example_list.Add(new ExampleClass
        {
            Surname = Console.ReadLine()
        });

    }
}

}

【讨论】:

    【解决方案2】:

    我可以在您的代码中看到其他问题。假设用户输入了错误的年龄值?那么你的程序就会崩溃。所以你必须要么继续尝试,要么抓住错误。

    List<ExampleClass> example_list = new List<ExampleClass>();
    string Name = Console.ReadLine();
    int Age;
    
    while (int.TryParse(Console.ReadLine(), out Age) == false)
    {
        Console.WriteLine("Enter valid age");
    }
    
    ExampleClass obj = new ExampleClass(Name, Age);
    example_list.Add(obj);
    

    您可以通过证明年龄限制等相关检查来增强此解决方案,它应该从 0 到 100 左右......您可以在 set 属性中处理这个问题,或者像我添加检查的方式一样。

    【讨论】:

      【解决方案3】:

      问题在于您没有使用为类ExampleClass 定义的构造函数。请注意,语法new ExampleClass { ... } 使用不带参数的默认类构造函数。在您的情况下,默认构造函数已被隐藏。您需要定义一个不带参数的新构造函数 (public ExampleClass()) 或使用现有的构造函数:

      使用定义的构造函数:

       example_list.Add(new ExampleClass
              (
                Name: Console.Readline(),
                Age: Convert.ToInt32(Console.Readline())
              ));
      

      对象初始化语法:

       public class ExampleClass 
          {
      
              public string Name {get; set;}
              public int Age {get; set;}
      
              public Example(string name, int age) {
              this.Name = name;
              this.Age = age; 
              }
              // constructor with no parameters will let you
              // use the Object Initializer Syntax without specifying 
              // the constructor explicitly
              public Example() {}
          }
      

      定义“默认”构造函数后,以下应该可以正常工作:

       example_list.Add(new ExampleClass
                  {
                    name = Console.Readline();
                    age = Convert.ToInt32(Console.Readline());
                  }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-08-09
        • 1970-01-01
        • 2019-02-06
        • 1970-01-01
        • 2018-05-16
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多