Is it possible to set multiple values in a set method?
I want to do something like the following:
public int ID { get; set => {Property = value, ID=value}; }
Is it possible to set multiple values in a set method?
I want to do something like the following:
public int ID { get; set => {Property = value, ID=value}; }
On
There is no need for => after the set you can use {..} instead. I think you are looking for a class like this:
class Sample
{
private int _Property;
private int _ID;
public int Property
{
get { return _Property; }
}
public int ID
{
get { return _ID; }
set
{
_ID = value;
_Property = value;
}
}
}
Here is an Example that shows the working,
Here the
Propertyis a read only property you cannot set its value, it will automatically assigned when you set the value forID
Expression-bodied setters don't have the expressive power to do more than one operation, so you need to use the full method body syntax:
It's reasonable to do this in some cases, because some operations have side effects by their very nature. For example, if you're setting an object's time zone property, it makes sense to alter the underlying
DateTimeto ensure that itsDateTimeKindisDateTimeKind.Local. If you don't, the object'sDateTimeproperty is incomplete or wrong.That said, if you find yourself doing this everywhere, you may want to rethink your design, because overuse is a code smell.