HELP~关于string数组的菜鸟问题 ……

yuanqi007 2005-05-04 12:43:34
using System;
namespace test
{
class Class1
{
public static string[] s;
[STAThread]
static void Main(string[] args)
{
s[0] = Console.ReadLine();
Console.WriteLine(s[0].ToString());
}
}
}
执行的时候,输入一个字符,然后提示出错:未将对象引用设置到对象的实例
希望各位指教,这是为什么呢?

小弟想实现,不定义String数组的长度,动态给该数组赋值,比如:
string [] s;
for(i = 0;1 < n;i ++)
{
s[i] = "ttt";
}
这样可以么?
谢谢先
...全文
77 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
lgg06 2005-05-04
  • 打赏
  • 举报
回复
string 不可以动态声明他的大小
可以考虑用arraylist 或者声明一个大点的
saucer 2005-05-04
  • 打赏
  • 举报
回复
no, you cannot, you need to do

string [] s = new string[n];

first

or use System.Collections.ArrayList

ArrayList al = new ArrayList();
al.Add("xx");
al.Add("yy");
...
string[] s = (string[])al.ToArray(typeof(string));
xiaoyur 2005-05-04
  • 打赏
  • 举报
回复
s是一个数组类型 没有初始化
动态数组 可以用Array.CreateInstance方法实现
yuanqi007 2005-05-04
  • 打赏
  • 举报
回复
恩,问题解决了,谢谢大家的帮助!

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧