c#将字符串放入数组中

Jul*_*eno 0 c# arrays string

这可能非常简单,但是如何将字符串放置或转换为数组呢?

我拥有的代码如下:

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string one;
        string[] two;

        one = "Juan";
        two = {one}; // here is the error

        HttpContext.Current.Response.Write(two);
    }
}
Run Code Online (Sandbox Code Playgroud)

并且错误如下: 编译器错误消息:CS0029:无法将类型'string'隐式转换为'string []'

谢谢你的帮助!

Hab*_*bib 5

替换这个:

two = {one}; // here is the error
Run Code Online (Sandbox Code Playgroud)

同

two = new[] { one }; 
Run Code Online (Sandbox Code Playgroud)

要么

two = new string[] { one };
Run Code Online (Sandbox Code Playgroud)

从错误消息中可以清楚地看到错误的原因.

请参阅:对象和集合初始化程序(C#编程指南)

稍后当你做的时候Response.Write,你将获得System.String[]输出,因为它two是一个数组.我想你需要用一些分隔符分隔的所有数组元素.你可以试试:

HttpContext.Current.Response.Write(string.Join(",", two));
Run Code Online (Sandbox Code Playgroud)

这将生成以逗号分隔的数组中的所有元素