方法中的可选参数不是带有Rectangle的编译时常量错误 [英] Optional parameter in method not compile-time constant error with Rectangle

查看:65
本文介绍了方法中的可选参数不是带有Rectangle的编译时常量错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一种方法,希望使用默认值(1,1,1,1)的Rectangle可选参数.

I have a method where I'd like to use a Rectangle optional parameter with default value of (1,1,1,1).

void Method(int i, int j = 1, Rectangle rect = new Rectangle(1,1,1,1)) {} //error

我该如何解决? (我正在使用XNA,所以它是Microsoft.Xna.Framework.Rectangle.)

How do I resolve this? (I'm using XNA, so it's a Microsoft.Xna.Framework.Rectangle.)

推荐答案

您没有.可选参数必须是编译时间常数,而new Rectangle(1,1,1,1)不是编译时间常数.

You don't. optional parameters must be compile time constants, and new Rectangle(1,1,1,1) isn't a compile time constant.

您可能有两种方法重载,一种没有矩形:

You could have two method overloads, one that doesn't have a rectangle:

void Method(int i, int j = 1) 
{
    Method(i, j, new Rectangle(1,1,1,1)) 
}

这篇关于方法中的可选参数不是带有Rectangle的编译时常量错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆