C# properties, never try to be to smart …

Veröffentlicht von

Writing my last C# customer code I tried to do smart things in a property. The property looked like this:

        public Guid ApplicationLayerCallId
            get => _applicationLayerCallId;
                _applicationLayerCallId = value;
                this.SipCallId = value.ToString();

        public string SipCallId { get { return this.Header.CallId; } set { this.Header.CallId = value; } }

My initial thought was that the ApplicationLayerCallId and the SipCallId will nearly always be the same. But over time things changed, and in some scenarios,  the SipCallId and the ApplicationLayerCallId have to have different values. One of the scenarios was:

void SomeMethod(IRequest req)
    this.SipCallId = req.CallId;
    this.ApplicationLayerCallId = Guid.NewGuid();

After an hour of debugging (or so), I recognized that the SipCallId was overwritten by ApplicationLayerCallId assignment …


Personal lessons learned: „Never ever assign one property in the setter of another property!“


Kommentar hinterlassen

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.