微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c# – 字节数组上的可选方法参数

我如何为字节数组参数设置认值?
下面的代码不会工作……

public static void init(SomeByteArray[] = {1,2,3,4}) {
    //Do Something
}

这可能吗?

我在.Net Micro Framework 4.1中这样做,如果它有任何区别……

解决方法

MSDN开始:

A default value must be one of the following types of expressions:

  • a constant expression;
  • an expression of the form new ValType(),where ValType is a value type,such as an enum or a struct;
  • an expression of the form default(ValType),where ValType is a value type.

因此,数组实例不能用作认值.

最好的解决方案是probbaly两个定义两个重载如下:

public static void Init()
{
    Init(new byte[] { 1,4 });
}

public static void Init(byte[] data)
{
    ...

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐