在集合 属性 中使用 console.ReadLine()

Use console.ReadLine() inside set property

我是一名初学者编码员,目前正在学习 c#,我想知道是否可以在 属性 的集合部分使用 Console.ReadLine(),然后像读取用户输入的方法,如下:

class Employee
{
    protected int empID;
    public int EmployeeID
    {
        set
        {
            Console.WriteLine("Please enter Employee ID:");
            this.empID = int.Parse(Console.ReadLine());
        }
    }
    //more code here
}
class Program
{
    static void Main(string[] args)
    {
        Employee employee1 = new Employee();
        employee1.EmployeeID;
        //more code here
    }
}

或者唯一的选择是在"Main"中直接使用Console.ReadLine(),如下:

class Employee
{
    protected int empID;
    public int EmployeeID { set; }
    //more code here
}
class Program
{
    static void Main(string[] args)
    {
        Employee employee1 = new Employee();
        employee1.EmployeeID = int.Parse(Console.ReadLine());
        //more code here
    }
}

预先感谢您的所有回答!


谢谢大家的回答!我现在可以看出这是一种错误的代码编写方式,我明白为什么。我认为通过在 'set' 属性 中使用 'Console.ReadLine();' 可以更容易地从用户那里获取值,并且我不必重写这部分:'

Console.WriteLine("Please enter Employee ID:");
this.empID = int.Parse(Console.ReadLine());

每次我都会要求用户输入。 但我现在明白为什么不应该使用它了。
再次感谢您的所有回答,祝您有愉快的一天!

是的,您可以将 Console.ReadLine() 放入集合中。但这是非常错误的。

C# 属性的编译类似于方法,因此您可以将任何可用的 C# 代码放入 属性,编译器将允许您这样做。 (您的代码中的问题是您没有为集合编写正确的调用)。

但是在良好的实践中思考 S.O.L.I.D,这是非常错误的。您的第二个代码片段看起来好多了。

已编辑:关于您的代码,

如果您 运行 您的代码与您编写的完全一样,我会注意到您的消息 "Please enter Employee ID:" 永远不会显示。发生这种情况是因为对 属性.

getset 方面存在误解

查看此特定行:

employee1.EmployeeID;

这行代码是对 属性 EmployeeIDget 调用。也许这并不明显,因为您没有使用 goted 值。但是这一行类似于:

var notUsedVar = employee1.EmployeeID;

要使用 属性 的 set 操作,您需要像这样的归因操作:

employee1.EmployeeID = 0; // or
employee1.EmployeeID++; // or
employee1.EmployeeID--; // or
employee1.EmployeeID += 1; // and so on...

代码段 ps:第一行你有一个 set 操作的调用,但你下面的行有一个 get 调用和 [=17= 之后] 打电话。

这里有一些代码片段让你确认并理解我在说什么:

class Employee
{
    private int _employeeID;

    public int EmployeeId
    {
        get
        {
            Console.WriteLine("The Employee.EmployeeId get operation was called.");
            return _employeeID;
        }
        set
        {
            Console.WriteLine("The Employee.EmployeeId set operation was called.");
            _employeeID = value;
        }
    }
}

class Program
{
    public static void Main()
    {
        var e = new Employee();

        e.EmployeeId++; // or any other exaple.
    }
}

如果你运行这个代码你会得到输出:

The Employee.EmployeeId get operation was called.
The Employee.EmployeeId set operation was called.

你走错方向了。 正确的方法是通过 Main 方法。

或者,如果您想出于任何原因将功能放入 class,应该是这样的

class Employee
{
    protected int empID;
    public int EmployeeID 
    {
        get { return empId; }
    }
    //more code here
    public void AskEmployeeID()
    {
        Console.WriteLine("Please enter Employee ID:");
        this.empID = int.Parse(Console.ReadLine());
    }
}

现在您可以在 Employee 对象上调用此函数作为 employee1.AskEmployeeID();

public class Employee
{
    public int EmployeeID { get; set; }
}
class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Please enter Employee ID:");

        var empID = int.Parse(Console.ReadLine());

        var employee1 = new Employee
        {
            EmployeeID = empID
        };
    }
}

GettersSetters 应该只用于 set/return 属性 持有的任何值。您还可以创建私有字段并使用不同的方法设置它们。但是,您不会从 class 调用 Console.ReadLine()。 class 代表您的实体。