1

Came across this,

public IConfiguration Configuration { get; }

public Startup(IConfiguration configuration)
{
    Configuration = configuration;
}

Which I've never really seen before. After investigating, I found a question on omitted setters vs private setter and it has me thinking it's the property equivalent of readonly.

Is this correct? What are the differences?

Shelby115
  • 2,816
  • 3
  • 36
  • 52
  • 2
    Hi when a field is `readonly` it can't be assigned to another value (outside of the declaration or constructor). – jspcal Jun 05 '19 at 18:54
  • Prior to (IIRC) C#6, you couldn't set a property with an omitted setter even in the constructor. A private setter was the only way to do that in earlier versions of the language. – 15ee8f99-57ff-4f92-890c-b56153 Jun 05 '19 at 18:55
  • Ah, I was focusing my searching around "no setter" instead of just "getter". I figured it had been asked before, I just wasn't finding it. – Shelby115 Jun 05 '19 at 19:00
  • It looks like it is calling the setter, but it's not (because it doesn't *have* a setter). It assigns the value directly to the underlying (readonly) variable. – Dennis_E Jun 05 '19 at 19:07

0 Answers0