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)
{
    ...

相关文章

目录简介使用JS互操作使用ClipLazor库创建项目使用方法简单测...
目录简介快速入门安装 NuGet 包实体类User数据库类DbFactory...
本文实现一个简单的配置类,原理比较简单,适用于一些小型项...
C#中Description特性主要用于枚举和属性,方法比较简单,记录...
[TOC] # 原理简介 本文参考[C#/WPF/WinForm/程序实现软件开机...
目录简介获取 HTML 文档解析 HTML 文档测试补充:使用 CSS 选...