首页 > 解决方案 > 如何在 C# 中使用多个可选参数

问题描述

我有这样的方法:

public static void MyMethod(int a, String opt1 = null, byte[] opt2 = null)
{
//code 
} 

我想使用 opt1 调用一次 MyMeythod,然后使用 opt2 调用一次;

MyMethod(3,"param");通话有效,但

MyMethod(3,new byte[]);不编译

有没有办法做到这一点,或者我应该明确传递 opt1 null

标签: c#optional-parameters

解决方案


使用名称:

MyMethod(3, opt2: new byte[]);

推荐阅读