我无法在 Program.cs 中实例化我的 Student 类

问题描述 投票:0回答:1

我的控制台应用程序中有一个 Student 类,如下所示:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp1
{
    public class Student
    {
        public Student(Student student)
        {
            StudentId = student.StudentId;
            FullName = student.FullName;
        }

        public int StudentId { get; set; }
        public string FullName { get; set; }
        
    }
}

它的主体中有一个复制构造函数,当我想在 Program 类中实例化它时,如下所示:

Student student = new Student();

我会遇到一个错误

“没有给出与所需的正式形式相对应的论点 ‘Student.Student(Student)’的参数‘student’”

当我无法实例化我的 Student 类时,如何将 Student 的参数传递给其构造函数?

我想实例化我的 Student 类,但出现了我所描述的错误。

c# .net console-application copy-constructor instant
1个回答
0
投票

一旦你编写了某种构造函数 - 在你的例子中是复制构造函数, 然后删除默认构造函数, 所以当你这样做时:

Student student = new Student();
它没有空的构造函数可供引用。 您的解决方案是简单地创建一个默认构造函数,用您选择的默认值初始化参数

public Student()
    {
        StudentId = 0;//for example
        FullName = "Jhon Doe";//for example
    }
© www.soinside.com 2019 - 2024. All rights reserved.