Question

The BindableBase abstract base class in a WinRT project is defined as follows :

[Windows.Foundation.Metadata.WebHostHidden]
public abstract class BindableBase : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    protected bool SetProperty<T>(ref T storage, T value, [CallerMemberName] String propertyName = null)
    {
        if (object.Equals(storage, value)) return false;

        storage = value;
        this.OnPropertyChanged(propertyName);
        return true;
    }

    protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        var eventHandler = this.PropertyChanged;
        if (eventHandler != null)
        {
            eventHandler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

That's fine.

Now I see many articles trying to implement this class, doing stuff like this :

private  int _timeEstimate;
        public int TimeEstimate
        {
            get { return this._timeEstimate; }
            set { this.SetProperty(ref this._timeEstimate, value); }
        }

the _timeEstimate is not initialized, how can it be passed using ref ?! is there something I'm missing ? this is really frustrating me, what am I missing, I even find the same writing in Microsoft's Exam-Ref preparation books !

Was it helpful?

Solution

_timeEstimate is a field. Fields are explicitly initialized to their zero values during the construction of a class (before the constructor fires). For a struct, they must be explicitly initialized in the constructor, or are zero'd if the type is initialized with the default constructor (side note: technically struct don't have default constructors, but C# and IL disagree over this, so we'll just call new SomeStruct() a constructor for convenience ;p)

Basically: it is initialized.

It is local variables that are not initialized.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top