[英]ref keyword when passing uninitialized parameter to a method
WinRT
项目中的BindableBase
抽象基类定义如下:
[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));
}
}
}
没关系。
现在我看到许多文章试图实现这个类,做这样的事情:
private int _timeEstimate;
public int TimeEstimate
{
get { return this._timeEstimate; }
set { this.SetProperty(ref this._timeEstimate, value); }
}
_timeEstimate没有初始化,如何使用ref
传递? 有什么我想念的吗? 这真让我感到沮丧,我错过了什么,我甚至在微软的考试准备书中找到了同样的写作!
_timeEstimate
是一个字段。 在构造class
期间(在构造函数触发之前),字段显式初始化为零值。 对于struct
,它们必须在构造函数中显式初始化,或者如果使用默认构造函数初始化类型则为零(旁注:技术上struct
没有默认构造函数,但C#和IL不同意这一点,所以我们为方便起见,只需将new SomeStruct()
称为构造函数; p)
基本上:它已初始化。
它是未初始化的局部变量 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.