将方法调用中的字符串数组初始化为C#中的参数 [英] Initializing a string array in a method call as a parameter in C#

查看:101
本文介绍了将方法调用中的字符串数组初始化为C#中的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有这样的方法:

public void DoSomething(int Count, string[] Lines)
{
   //Do stuff here...
}

为什么我不能这样称呼它?

Why can't I call it like this?

DoSomething(10, {"One", "Two", "Three"});

什么是正确的(但希望不是很长的路要走)?

What would be the correct (but hopefully not the long way)?

推荐答案

您可以执行以下操作:

DoSomething(10, new[] {"One", "Two", "Three"});

如果所有对象的类型相同,则无需在数组定义中指定类型

provided all the objects are of the same type you don't need to specify the type in the array definition

这篇关于将方法调用中的字符串数组初始化为C#中的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆