8

I get the "only auto-implemented properties can have initializers in C#" error when trying to do the following:

   public int Precision
    {
        get { return Precision; } 
        set
        {
            if (value < 0)
                Precision = 0;
            else if (value > 15)
                Precision = 15;
            else
                Precision = value;
        }
    } = 12;

Why is this not allowed?

2
  • 6
    A bigger issues is that you are recursively calling the property. You should create a backing field and that is what you should initialize. Note that auto-implemented properties will create a backing field when compiled so the only want to initialize them is by initializing the property itself. Commented Feb 28, 2020 at 21:35
  • 2
    I'd imagine it's something to do with the fact that the auto-initialisation can't access this since auto-initialisation happens before the object is fully constructed, whereas your setter can. Also the setter probably can't run because the object isn't fully instanced either, so what do you want the auto-initialisation to set? If you have no backing field (because your setter doesn't set one) what is it supposed to do? (and also the above, you should have a private backing field which is what an auto-property has behind the scenes) Commented Feb 28, 2020 at 21:38

2 Answers 2

11

Pretty sure that is not really how you use get and set. Plus your get suffers from self reference. I think this is what you want:

private int _precision = 12;
public int Precision {
    get => _precision;
    set {
        if (value < 0)
            _precision = 0;
        else if (value > 15)
            _precision = 15;
        else
            _precision = value;
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

@Izzo how does this answer "why are initializers only allowed for auto-properties"?
@HimBromBeere While your answer was more technically correct, kaleidawave's indicated more concisely that i was forgetting a backing field.
8

Well, an auto-property is just syntactic sugar for a property that gets and sets an automatically created backing-field. So the following two code-segments are similar:

public int Precision { get; set; }

and

public int Precision
{
    get { return <Precision>k__BackingField; } 
    set { <Precision>k__BackingField = value; }
}

However when you create some own logic within your property, there´s no such thing as an automatically backing-field. In fact you could even do the following without any backing-field:

set { Console.WriteLine(); }

An initial value however is resolved to the following constructor:

MyClass()
{ 
    this.<Precision>k__BackingField = myValue; 
}

However when there is no such backing-field, what should the compiler do here?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.