C# 在set属性中使用console.ReadLine()

C# 在set属性中使用console.ReadLine(),c#,properties,console,console.readline,C#,Properties,Console,Console.readline,我是一名初学者,目前正在学习c#,我想知道是否可以在属性的set部分中使用Console.ReadLine(),然后像读取用户输入的方法一样使用它,如下所示: class Employee { protected int empID; public int EmployeeID { set { Console.WriteLine("Please enter Employee ID:"); th

我是一名初学者,目前正在学习c#,我想知道是否可以在属性的set部分中使用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
    }
}
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
    }
}
或者,唯一的选择是直接在“Main”中使用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
    }
}
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
    }
}
提前感谢您的所有回答


谢谢大家的回答!我现在明白了,这是一种错误的代码编写方式,我明白了原因。我认为通过使用“Console.ReadLine();”在“set”属性中,从用户处获取值会更容易,我不必重新编写此部分:'

Console.WriteLine("Please enter Employee ID:");
this.empID = int.Parse(Console.ReadLine());
每次我都会要求用户输入。 但我现在明白了为什么不应该使用它。

再次感谢您提供的所有答案,祝您度过愉快的一天

是的,您可以将
控制台.ReadLine()
放在集合中。但这是非常错误的

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

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

编辑:关于您的代码,

如果您完全按照编写的方式运行代码,我会注意到您的消息
“请输入员工ID:”
“永远不会显示。这是因为对属性的
get
set
方面存在误解

看看这一行:

employee1.EmployeeID;
这行代码是对属性
EmployeeID
get
调用。可能这并不明显,因为您没有使用goted值。但这条线类似于:

var notUsedVar = employee1.EmployeeID;
要使用属性的
set
操作,您需要一个属性操作,如:

employee1.EmployeeID = 0; // or
employee1.EmployeeID++; // or
employee1.EmployeeID--; // or
employee1.EmployeeID += 1; // and so on...
Snippet ps:您对
set
操作进行单个调用的第一行,但下面的行既有
get
调用,又有
set
调用

下面是一些剪断的代码,让您确认并理解我的意思:

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.
    }
}
如果运行此代码,您将获得以下输出:

已调用Employee.EmployeeId获取操作。
调用了Employee.EmployeeId设置操作


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

或者,无论出于何种原因,如果您想将功能放在类中,应该是这样

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()


getter
Setters
只能用于设置/返回属性所包含的任何值。您还可以创建私有字段,并使用不同的方法设置这些字段。但是,您不会从类中调用
Console.ReadLine()
。类是实体的表示。

选项2是最好的。第一个选项是访问
get
(未定义),因此实际上会得到一个错误。尽管您可以执行类似于
employee1.EmployeeID=1的操作
相反,
1
将被抛出,提示将出现并接受一个值,这真的是一个糟糕的设计。我会诚实地使用get和set,然后在属性设置的外面解析值,作为一个简单的例子,关于我所说的签出这个之前的帖子第一个选项感觉很奇怪。。。在我看来,它更难阅读,因为您的
Main
代码看起来像是该属性什么都没做(尽管它不是),这让您在其他地方寻找甚至是一点点的理解。如果我没有给出上下文,而是说
myClass.SomeProperty,你认为它在做什么?你真的只能猜测。这个答案是非常错误的,充其量应该是一个评论。。如果它是
非常错误的
,如您所述,那么发布一些支持您的注释的代码,显示OP应该如何使用getter和setter为属性赋值。这个答案是基于在属性的set部分中使用Console.ReadLine()是否可能的问题,代码中没有明确说明。这完全有可能,但你所说的答案是肯定的,但这是非常错误的,不是一个好答案。。既然你编辑了答案,它看起来100%好了。。然而,就个人而言,你应该向OP展示两种方式的示例。
1
错误方式与
2
正确方式。。这就是我想说的……明白了,我想,请再读一遍,然后告诉我你觉得怎么样。我试着纠正你所说的。看看@LuisLavien answer,这是一个很好的例子,说明了如何做到清晰、简单,直到一个坏主意,因为你现在已经在你的Employee类中引入了控制台依赖项。它使单元测试变得更加困难,更不用说在控制台应用程序之外重用Employee类几乎是不可能的(如果不进行重构的话)。@Nate222我不认为创建依赖关系是OP的问题,这是对初学者的回答,OP将在console world中停留足够长的时间来学习解决方法。感谢您的回答!