C#中的NullableMethodParametersDuplicateQuestionPassingNullParameterstoC#MethodsCanIdothisinc#for.Net2.0?publicvoidmyMethod(stringastring,int?anint){//一些代码,其中我可能有一个int可以使用//或者我可能没有...}如果没有,我可以做类似的事情吗?是的,假设您故意添加了V形符号,并且您的意思是:publicvoidmyMethod(stringastring,int?anint)anint现在将具有HasValue属性。取决于你想要达到的目标。如果您希望能够删除anint参数,则必须创建一个重载:publicvoidmyMethod(stringastring,intanint){}publicvoidmyMethod(stringastring){myMethod(astring,0);//或anint的其他默认值}您现在可以这样做:myMethod("boo");//等同于myMethod("boo",0);我的方法(“嘘”,12);如果您想传递可为空的int,请参阅其他答案。;)在C#2.0中你可以这样做;publicvoidmyMethod(stringastring,int?anint){//一些代码,其中我可能有一个int可以使用//或者我可能没有...}并调用上面的方法是C#学习教程:所有内容由C#中的可空方法参数共享。如果对大家有用,需要进一步了解C#学习教程,希望大家多多关注——myMethod("Hello",3);myMethod("你好",null);本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如需转载请注明出处:
