Why can't I use Point and Rectangle as optional parameters?

I am trying to pass an optional argument to a geometry function called offsetthat may or may not be specified, but C # does not allow me to do one of the following: Is there any way to do this?

  • Default zero

    Error: a value of type '' cannot be used as the default parameter, because there are no standard conversions for entering "System.Drawing.Point"

    public void LayoutRelative(.... Point offset = null) {}
    
  • Empty by default

    Error: The default parameter value for 'offset' must be a compile-time constant

    public void LayoutRelative(.... Point offset = Point.Empty) {}
    
+5
source share
1 answer

, . default:

public void LayoutRelative(.... Point offset = default(Point)) {}

:

public void LayoutRelative(.... Point? offset = null)
{
    if (offset.HasValue)
    {
        DoSomethingWith(offset.Value);
    }
}
+11

All Articles