可选参数和命名参数
为什么这两个要一起讲,因为这两个常常一起出现,可选参数常常用来替代以前的重载,但是当多个可选参数出现的时候,你想显示传递的参数只有一个,这个时候编译器不知道你想给那个参数传递参数,就需要指定传递那个参数的名字,这个就是命名参数
private RectangleFigure CreateFigure(Color color, PointF pointF = new PointF(), SizeF sizeF = new SizeF(),int priority = basicPriority) { color = Color.FromArgb(alpha, color.R, color.G, color.B); RectangleFigure figure; if (pointF.IsEmpty || sizeF.IsEmpty) { float w = Math.Min(canvas.ClientRectangleWorld.Width, backDie.Width) / 10; float h = Math.Min(canvas.ClientRectangleWorld.Height, backDie.Height) / 10; float x = (canvas.ClientRectangleWorld.Left + canvas.ClientRectangleWorld.Right - w) / 2; float y = (canvas.ClientRectangleWorld.Top + canvas.ClientRectangleWorld.Bottom - h) / 2; figure = new RectangleFigure(new RectangleF(x, y, w, h)); } else { figure = new RectangleFigure(new RectangleF(pointF, sizeF)); } figure.nLayer = priority; }
比如我只想传递Priority 参数,那么就必须要用命名参数
editingRectangle = CreateFigure(color,priority:priority);

浙公网安备 33010602011771号