Theoptionalparameterinthemethodisnotaconstantcompile-timeerrorusingRectangleis(1,1,1,1).voidMethod(inti,intj=1,Rectanglerect=newRectangle(1,1,1,1)){}//error我该如何解决这个问题?(我使用的是XNA,所以它是Microsoft.Xna.Framework.Rectangle。)你不需要。可选参数必须是编译时常量,而newRectangle(1,1,1,1)不是。你可以有两个方法重载,一个没有矩形:voidMethod(inti,intj=1){Method(i,j,newRectangle(1,1,1,1))}我刚刚找到了一个更好的方法:voidMyMethod(stringsomeString,Rectanglerect=default(Rectangle)){if(rect==default(Rectangle))rect=newRectangle(1,1,1,1);}可能只有一个问题:当默认值和传递值匹配时,==default(T)仍然为真。但解决方法是传递null并检查它以将其设置为默认ot类型。以上是C#学习教程:方法中的可选参数不是用Rectangle编译时常报错分享的所有内容。如果对你有用,需要了解更多C#学习教程,希望大家多加关注——本文来自网络合集,不代表立场,如涉及侵权,请右击联系管理员删除。如需转载请注明出处:
