Is it possible to assign return value for getter without private backing field C#

61 views Asked by At

I come across the following example:

public class TimeService
{
    public TimeService()
    {
        Time = DateTime.Now.ToLongTimeString();
    }
    public string Time { get; }
}

We have properties for which, e.g., the following is an example of initializing with empty string: public string StringProperty { get; set; } = "";, but how does the Time from TimeService work if getter properties cannot be assigned values directly?

Maybe, I haven't formulate my question properly when browsing, but I haven't found the appropriate answer yet.

0

There are 0 answers