How do I set up the Get property?
The closest you can get is that in C#
6 and later, you can initialize auto-implemented properties in a similar manner to fields:
public DateTime FirstName { get; set; } = // your value here;
if you want to execute some logic before assignment, you can create a method to execute the logic and assign the result like this:
public DateTime FirstName { get; set; } = doLogic();
The last option will be just classic get;
set;
public DateTime lastprocessed
{
get
{
// some logic ...
return // some value;
}
set
{
// some logic ...
}
}
source to share
I think it is best to use either what @Alisson suggested in the comment ( protected get
with an accessor method for its value) or private
:
private DateTime _lastprocessed;
public DateTime lastprocessed
{
get
{
return _lastprocessed.ToLocalTime().ToUniversalTime();
}
set
{
_lastprocessed = value;
}
}
When you apply formatting to an accessory get
with no backing field and try to create an accessory set
, you will get an error because it set
will call itself endlessly trying to set the property value.
If you add an initializer to the auto property, it's okay to set an initial value lastprocessed
, but I think you end up with a problem assuming that you intend to reassign the value lastprocessed
. Even if you define a function for the initializer, there is no way for you to access the value that is being assigned lastprocessed
.
source to share