问:字符串数组 [英] Q: String array

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

问题描述

嗨!


有一个简单的问题。我是c#的新手,但这并没有让我任何意义。


如果我声明:string [] myStringArray = new string [0];


如何用多个元素填充它?


问候


马丁

Hi!

Got a simple question. I am new to c# but this is not making me any sence.

If i declare: string[] myStringArray = new string[0];

How the heck could i fill it with more than one element?

Regards

Martin

推荐答案

" Visual Systems AB(Martin Arvidsson)" < ma ************** @ vsab.netwrote在

消息新闻:eD ************** @ TK2MSFTNGP04 .phx.gbl ...
"Visual Systems AB (Martin Arvidsson)" <ma**************@vsab.netwrote in
message news:eD**************@TK2MSFTNGP04.phx.gbl...

嗨!


有一个简单的问题。我是c#的新手,但这并没有让我任何意义。


如果我声明:string [] myStringArray = new string [0];


如何用多个元素填充它?


问候


Martin
Hi!

Got a simple question. I am new to c# but this is not making me any sence.

If i declare: string[] myStringArray = new string[0];

How the heck could i fill it with more than one element?

Regards

Martin



你是对的,你不能用多个元素填充它,但作为一个字符串

数组,你可以填充的单个元素可以和你想要的一样多的字符(理智之内)。

所以:


string [] myStringArray = new string [ 0];

myStringArray [0] ="一个很好的长字符串可以很高兴地去这里......" ;;

myStringArray [1] ="另一个字符串?呃,它不会喜欢这个..." ;;


第二个赋值会导致运行时错误,因为数组只定义了
单个元素。


也许你正在考虑一个字符串是一个

字符数组的事实......?


Chris。

You''re right, you can''t fill it with more than one element, but as a string
array, the single element you CAN fill it with can be as many characters as
you want (within reason).
So:

string[] myStringArray = new string[0];
myStringArray[0]="A Nice Long string could quite happily go here...";
myStringArray[1]="Another string? UhOh, it is not going to like this...";

The second assignment would cause a runtime error as the array has only been
defined with a single element.

Maybe you are thinking of the fact that a string is kind of an array of
characters...?

Chris.


嗨Martin,


如果你需要添加和/或删除行从数组中,使用ArrayList或

a泛型列表< Tinstead。


对于常规数组,大小(长度)在创建时被锁定,不能

改变了。

2006年9月19日星期二15:39:05 +0200,Visual Systems AB(Martin Arvidsson)

< ma *** ***********@vsab.netwrote:
Hi Martin,

If you need to add and/or remove lines from an array, use an ArrayList or
a generic List<Tinstead.

For regular arrays size (length) is locked upon creation and cannot be
changed.
On Tue, 19 Sep 2006 15:39:05 +0200, Visual Systems AB (Martin Arvidsson)
<ma**************@vsab.netwrote:

嗨!


有一个简单的问题。我是c#的新手,但这并没有让我任意

sence。


如果我声明:string [] myStringArray = new string [0];


如何用多个元素填充它?


问候


Martin

Hi!

Got a simple question. I am new to c# but this is not making me any
sence.

If i declare: string[] myStringArray = new string[0];

How the heck could i fill it with more than one element?

Regards

Martin




-

快乐编码!

Morten Wennevik [C#MVP ]



--
Happy Coding!
Morten Wennevik [C# MVP]


" Visual Systems AB(Martin Arvidsson)" < ma ************** @ vsab.netwrote
消息新闻中的
:eD ************** @ TK2MSFTNGP04 .phx.gbl ...
"Visual Systems AB (Martin Arvidsson)" <ma**************@vsab.netwrote
in message news:eD**************@TK2MSFTNGP04.phx.gbl...

嗨!


有一个简单的问题。我是c#的新手,但这并没有让我任意

sence。


如果我声明:string [] myStringArray = new string [0];


如何用多个元素填充它?
Hi!

Got a simple question. I am new to c# but this is not making me any
sence.

If i declare: string[] myStringArray = new string[0];

How the heck could i fill it with more than one element?



实际上,你不能放任何东西它...它的大小为0

新字符串[1]会保留一个字符串

新字符串[0]将保持零字符串


假设你需要一个数组,但你不知道它将持有多少元素。无论出于何种原因,您不希望使用ArrayList或其他一些动态集合。一个大小为零的数组比一个空引用更好地默认为

。一旦你知道了数据,就可以用一个大小为0的数组替换空数组

,其中包含真实数据


然后你就可以在没有检查的情况下写出这样的东西for null

foreach(myStringArray中的字符串str)

{

Blah();

}


即使阵列的大小为0,这也能正常工作。


希望这有助于

比尔

Actually, you can''t put anything in it...it has a size of 0
new string[1] would hold one string
new string[0] would hold zero strings

Suppose you need an array but you don''t know how many elements it will
hold. For whatever reason you don''t wish to use an ArrayList or some
other dynamic collection. a size zero array makes a better default than
a null reference. Once you know the data you can replace the empty array
with an array of size 0 containing real data

then you could write something like this without a check for null
foreach (string str in myStringArray)
{
Blah();
}

This will work even if the size of the array is 0.

Hope this helps
Bill


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

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