构造函数中的字符串数组 [英] String array in constructor

查看:176
本文介绍了构造函数中的字符串数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我试着学习用C#编程。而且我有这个问题

关于为什么字符串数组在第一堂课中不起作用但是在第二节中它的作用是
。对我来说,看起来他们做的完全一样。


class Test

{

string [] words;

public Test(){

words = {"","","" }; //这不起作用

}

}


class Test2

{

string [] words;

public Test2(){

words = new string [3]; //这个有效

}

}

So Im trying to learn to program with C#. And I have this question
about why the string array won''t work in the first class but it does in
the second. To me it looks like they do the exact same thing.

class Test
{
string[] words;
public Test() {
words = { "", "", "" }; // This doesn''t work
}
}

class Test2
{
string[] words;
public Test2() {
words = new string[3]; // This works
}
}

推荐答案

kk

< ma ************** @ gmail.com>在消息中写道

news:11 ********************** @ f6g2000cwb.googlegro ups.com ...
kk
<ma**************@gmail.com> wrote in message
news:11**********************@f6g2000cwb.googlegro ups.com...
所以我试着学习用C#编程。我有这个问题
关于为什么字符串数组在第一类中不起作用但在第二类中它不起作用。对我来说,看起来他们做的完全一样。

上课测试
{
字符串[]字;
公共测试(){
字= {"","","" }; //这不起作用
}


类Test2
{
string [] words;
public Test2() {
words = new string [3]; //这有效
}
}
So Im trying to learn to program with C#. And I have this question
about why the string array won''t work in the first class but it does in
the second. To me it looks like they do the exact same thing.

class Test
{
string[] words;
public Test() {
words = { "", "", "" }; // This doesn''t work
}
}

class Test2
{
string[] words;
public Test2() {
words = new string[3]; // This works
}
}



在使用之前必须设置静态数组的长度


sring [] words = new String [3] {"","",""};


或string [] words = new String [3];

words [0] ="" ;;

words [1] ="" ;;

words [2] ="" ;;


< ma ************** @ gmail.com>在消息中写道

news:11 ********************** @ f6g2000cwb.googlegro ups.com ...
you must set the length of a static array before using it

sring[] words = new String[3] {"","",""};

or string[] words = new String[3];
words[0] = "";
words[1] = "";
words[2] = "";

<ma**************@gmail.com> wrote in message
news:11**********************@f6g2000cwb.googlegro ups.com...
所以我试着学习用C#编程。我有这个问题
关于为什么字符串数组在第一类中不起作用但在第二类中它不起作用。对我来说,看起来他们做的完全一样。

上课测试
{
字符串[]字;
公共测试(){
字= {"","","" }; //这不起作用
}


类Test2
{
string [] words;
public Test2() {
words = new string [3]; //这有效
}
}
So Im trying to learn to program with C#. And I have this question
about why the string array won''t work in the first class but it does in
the second. To me it looks like they do the exact same thing.

class Test
{
string[] words;
public Test() {
words = { "", "", "" }; // This doesn''t work
}
}

class Test2
{
string[] words;
public Test2() {
words = new string[3]; // This works
}
}



测试抱歉

< ma **** **********@gmail.com>在消息中写道

news:11 ********************** @ f6g2000cwb.googlegro ups.com ...
test sorry
<ma**************@gmail.com> wrote in message
news:11**********************@f6g2000cwb.googlegro ups.com...
所以我试着学习用C#编程。我有这个问题
关于为什么字符串数组在第一类中不起作用但在第二类中它不起作用。对我来说,看起来他们做的完全一样。

上课测试
{
字符串[]字;
公共测试(){
字= {"","","" }; //这不起作用
}


类Test2
{
string [] words;
public Test2() {
words = new string [3]; //这有效
}
}
So Im trying to learn to program with C#. And I have this question
about why the string array won''t work in the first class but it does in
the second. To me it looks like they do the exact same thing.

class Test
{
string[] words;
public Test() {
words = { "", "", "" }; // This doesn''t work
}
}

class Test2
{
string[] words;
public Test2() {
words = new string[3]; // This works
}
}



这篇关于构造函数中的字符串数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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