Reputation: 89
I have been taught in school about C#. We did some basic stuff like loops, if etc.
Now we do more about OOP. Teacher said us something about auto-implemented-property and I find this feature as great. But I am curious how can I set value of property via method.
When we didn't know auto-implemented-property. We always did a method to set or get value of class. But when I use auto-implemented-property I do not see any methods to get or set value of class instance. So how can I set the value of some property of class when I can set the value only via constructor. I want to know that, because when property is private I can set it only via constructor, which is not a problem, but what I can do when I want to set value via Console.Readline(); ?
namespace _001_dedicnost
{
class Car
{
int Size { get; set; }
}
class Program
{
static void Main(string[] args)
{
Car car1 = new Car(5);
// but the following line wont work
car1.Set(51);
}
}
}
Upvotes: 1
Views: 518
Reputation: 1211
If the class is a simple anemic model (without logic), set the property as public
, and it will work.
If you want to control the invariants (business rules), you'd want to have a public Size { get; private set; }
with a public void SetSize(int size) { /* ... */ }
which contains your business rules.
Here are three 'patterns' normally used in C#:
// Anemic domain model (simple entity)
public class Car
{
public int Size { get; set;}
}
// Domain model with business rules
public class Car
{
public int Size { get; private set; }
public void SetSize (int size)
{
// check to make sure size is within constraints
if (size < 0 || size > 100)
throw new ArgumentException(nameof(size));
Size = size;
}
}
// Value object
public class Car
{
public Car (int size)
{
// check constraints of size
Size = size;
}
public int Size { get; }
}
Upvotes: 0
Reputation: 914
This
public class Point {
public int X { get; set; } = 0;
}
is equivalent to the following declaration:
public class Point {
private int __x = 0;
public int X { get { return __x; } set { __x = value; } }
}
This means you have "couple of 'methods' under c sharp compilator which called using '=' sign"
Point p = new Point();
p.X = 10; //c# compiler would call something like p.__set_X(10)
int i = p.X; //c# compiler would call something like int i = p.__get_X();
Read more about auto-properties https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/classes#automatically-implemented-properties
Btw I dont recommend to use it - it breaks readability and refactobility of code ;(
Upvotes: 0
Reputation: 2879
Your class Car have PRIVATE property Size, so u cant't have access to it from your code, only from class CAR
If u want to set value to this property, u have to declare it PUBLIC:
class Car
{
public int Size { get; set; }
}
static void Main(string[] args)
{
Car car1 = new Car();
car1.Size = 1;
}
Upvotes: 1
Reputation: 3053
When you put the property on the left-hand side of an expression, the set method is automatically called on it with the right-hand side of the expression as the value.
So car1.Size = 51
is like calling the expanded setter for the Size property with value
being 51.
Upvotes: 0