-1

In C#, property can be declared with getter and setter. If there is no setter then it shouldn't allow to set?

But this .net core 3.1 example tutorial shows value assigned to such a property. How is this allowed?

Code:

public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }
      
    public IConfiguration Configuration {get;}
}
Cleptus
  • 3,446
  • 4
  • 28
  • 34
variable
  • 8,262
  • 9
  • 95
  • 215
  • 1
    Please edit your question to show the code as *text*. (I'm adding an answer, and it would have been much easier to copy/paste that code than retype it all...) – Jon Skeet Nov 26 '21 at 06:50
  • Ok I have added code now thanks. – variable Nov 26 '21 at 06:55
  • 1
    Imagine that this wasn't possible. **What** purpose do you think a property that can never be set would serve? – Damien_The_Unbeliever Nov 26 '21 at 07:48
  • Does this answer your question? [Automated property with getter only, can be set, why?](https://stackoverflow.com/questions/34743533/automated-property-with-getter-only-can-be-set-why) or [Property with no setter - how can it get set from constructor?](https://stackoverflow.com/q/46381483/150605) or [Setting a property without a setter. How is this not a compile error?](https://stackoverflow.com/q/41990987/150605) – Lance U. Matthews Nov 26 '21 at 08:34

1 Answers1

5

A read-only property can only be set:

  • If it's an auto-implemented property (i.e. the property body just has { get; })
  • When the setting the property is within the constructor

The read-only property is implemented as a read-only field, and when the property is set within the constructor, the read-only field is set directly.

So the code you've posted is equivalent to:

public class Startup
{
    private readonly IConfiguration _configuration;
    public IConfiguration Configuration => _configuration;

    public Startup(IConfiguration configuration)
    {
        _configuration = configuration;
    }
}
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • If the class was static, then rather than constructor I would have to set the value during property declaration? – variable Nov 26 '21 at 19:22
  • 1
    Or in a static constructor. At least so I'd expect - it's easy enough for you to try... – Jon Skeet Nov 26 '21 at 19:26
  • Assuming no need of constructor argument, are both (static constructor approach and property assignment) equally good options? – variable Nov 26 '21 at 19:53
  • @variable: I'd probably use a property initializer where it's feasible. – Jon Skeet Nov 27 '21 at 08:17
  • Coming back to the original question, 1. does the absence of setter implicitly add a private setter? – variable Nov 29 '21 at 06:42
  • 2. Why have you linked to readonly documentation? (That link says nothing about properties). – variable Nov 29 '21 at 06:44
  • @variable: I didn't add that link. Another user did. But Stack Overflow is not intended to be a constant dialog between question asker and answerer. If you have a follow-up question, ask a new one. But I'd suggest using ildasm first to find out what the IL looks like - and note that in my answer the equivalent code does *not* have a setter. – Jon Skeet Nov 29 '21 at 06:47