Deconstruct这个方法可以是扩展方法。
扩展方法
① 定义一个静态类,类名随便。
② 写一个static void Deconstruct方法,并且第一个参数必须是this关键词开头。
③然后接类名+参数,接out参数。
④使用的时候,直接可以在被扩展的类的实例中点出这个扩展方法。
举例:
public class Rectangle
{
public readonly float Width;public readonly float Height;
public Rectangle(float width, float height)
{
Width = width;
Height = height;
}
}
public static class Extensions
{
public static void Deconstruct(this Rectangle rect,out float width,out float height)
{
width = rect.Width+19;
height = rect.Height-2;
}
}
class Program
{
static void Main(string[] args)
{
var rect = new Rectangle(3, 4);
rect.Deconstruct(out var TestPara1, out var TestPara2);
Console.WriteLine(TestPara1 + " "+ TestPara2);
Console.ReadKey();
}
}
输出:
22 2
完整写法:
public class Rectangle
{
public readonly float Width;public readonly float Height;
public Rectangle(float width, float height)
{
Width = width;
Height = height;
}
}
public static class Extensions
{
public static void Deconstruct(this Rectangle rect,out float width,out float height)
{
width = rect.Width+19;
height = rect.Height-2;
}
}
class Program
{
static void Main(string[] args)
{
var rect = new Rectangle(3, 4);
Extensions.Deconstruct(rect,out var width,out var height);
Console.WriteLine(width + " "+ height);
Console.ReadKey();
}
}
输出:
22 2
Today's comments have reached the limit. If you want to comment, please wait until tomorrow (UTC-Time).
There is 04h26m55s left until you can comment.