I am a beginner coder, I am currently learning c# and I was wondering is it possible to use Console.ReadLine(), inside the set part of the property and then use it like a method to read the user input, as follows:
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
}
}
or the only option is to use Console.ReadLine() directly in the the "Main", as follows:
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
}
}
Thank you in advance for all answers!
Thank you all for your answers! I can see now that this is a wrong way to write a code and I understand why. I thought that by using 'Console.ReadLine();' inside the 'set' property it will be easier to get the value from the user and I will not have to re-write this part:'
Console.WriteLine("Please enter Employee ID:");
this.empID = int.Parse(Console.ReadLine());
each time I will ask the user for input.
But I understand now why It should not be used.
Thank you again for all the answers and have a nice day!
Yes you can put a Console.ReadLine()
inside a set. But this is very wrong.
C# properties are compiled similar to method, so you can put any available C# code inside a property, the compiler will allow you to do that. (The problem in your code is you're not writing the correctly call for the set).
But thinking in good practices and S.O.L.I.D, this is very wrong. Your second code snippet looks much better.
Edited: About your code,
if you run your code exactly like you wrote, I'll notice that your message "Please enter Employee ID:"
is never displayed. This happens because a misconception about the get
and set
aspects of a property.
Look at this specific line:
employee1.EmployeeID;
This line of code is a get
call on property EmployeeID
. Maybe it's not obvious because you're not using the goted value. But this line is similar to:
var notUsedVar = employee1.EmployeeID;
To use a set
operation of a property you NEED a attribution operation like:
employee1.EmployeeID = 0; // or
employee1.EmployeeID++; // or
employee1.EmployeeID--; // or
employee1.EmployeeID += 1; // and so on...
Snippet ps: the first line you have a single call to a set
operation, but the lines below you have both a get
call and after a set
call.
Here some snipped code to you confirm and understand what I'm saying:
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.
}
}
If you run this code you'll get the output:
The Employee.EmployeeId get operation was called.
The Employee.EmployeeId set operation was called.