protected ComplexType _propertyName; public ComplexType PropertyName { get { if (_propertyName == null) _propertyName = new ComplexType(); return _propertyName; } }
要在C中重复(因为有〜15个实例),我将使用一个预处理宏,但我注意到C#不支持它们.
我想知道有没有人有建议如何干净地在C#中做这个?
protected ComplexType _propertyName; public ComplexType PropertyName { get { return GetProperty(ref _propertyName); } } . . private T GetProperty<T>(ref T property) where T : new() { if (property == null) property = new T(); return property; }