将未初始化的参数传递给方法时的ref关键字

Aym*_*udi 4 c# ref mvvm windows-runtime

项目中的BindableBase抽象基类WinRT定义如下:

[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));
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

没关系.

现在我看到许多文章试图实现这个类,做这样的事情:

private  int _timeEstimate;
        public int TimeEstimate
        {
            get { return this._timeEstimate; }
            set { this.SetProperty(ref this._timeEstimate, value); }
        }
Run Code Online (Sandbox Code Playgroud)

_timeEstimate未初始化,如何使用ref?!有什么我想念的吗?这真让我感到沮丧,我错过了什么,我甚至在微软的考试准备书中找到了同样的写作!

Mar*_*ell 6

_timeEstimate是一个领域.在构造a class(构造函数触发之前)期间,字段被显式初始化为零值.对于a struct,它们必须在构造函数中显式初始化,或者如果使用默认构造函数初始化类型则为零(旁注:技术上struct没有默认构造函数,但C#和IL不同意这一点,所以我们将new SomeStruct()为方便起见,只需调用构造函数; p)

基本上:它已初始化.

它是未初始化的局部变量.