
上QQ阅读APP看书,第一时间看更新
How to do it...
- Consider the class SomeClass. It contains a constructor, finalizer, and a property.
public class SomeClass
{
private int _initialValue;
// Property
public int InitialValue
{
get
{
return _initialValue;
}
set
{
_initialValue = value;
}
}
// Constructor
public SomeClass(int initialValue)
{
InitialValue = initialValue;
}
// Finalizer
~SomeClass()
{
WriteLine("Release unmanaged code");
}
}
- With expression-bodied members, the class SomeClass can be simplified and the number of lines of code reduced.
public class SomeClass
{
private int _initialValue;
public int InitialValue
{
get => _initialValue;
set => _initialValue = value;
}
public SomeClass(int initialValue) =>
InitialValue = initialValue;
~SomeClass() => WriteLine("Release unmanaged code");
}