小编vbb*_*ett的帖子

是否可以将元素直接转换为数组?

有没有办法将单个对象T转换为T[]

使用它的一个例子是在将单个传递给string需要a的函数时string[]

void ExistingMethod(string[] sa);
void main()
{
  string s;
  ExistingMethod(s); //<= problem is there a syntax that allows this type of cast?
}
Run Code Online (Sandbox Code Playgroud)

对这样的解决方案不感兴趣

string [] singleElementArray = new string[1];
singleElementArray[0] = s;
ExistingMethod(singleElementArray)
Run Code Online (Sandbox Code Playgroud)

我想看看C#是否允许这种类型的铸造.

我以为我看到了Java允许它的一种方式,只需([s])用[] 将它包装起来即可.C#有这种语法吗?

注意,不想创建1的数组并分配它...

c# casting

2
推荐指数
1
解决办法
309
查看次数

标签 统计

c# ×1

casting ×1