C#中为另一个类中的静态数组赋值异常

Rocco_Cui 2018-08-07 04:25:36
//////A方法
public class test
{
public Class s
{
public string str;
public int number;
}

public static s[] t=new s[4]
}
//////////////


///////方法B
using ******;

public Class Do
{
test.t[0].str="Hello";
test.t[1].str="World";
test.t[2].str="Oh";
test.t[3].str="God";

test.t[0].number=1;
test.t[1].number=2;
test.t[2].number=3;
test.t[3].number=4;
}

会出现System.NullReferenceException:“未将对象引用设置到对象的实例。”的异常.
...全文
127 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
Rocco_Cui 2018-08-07
  • 打赏
  • 举报
回复
引用 3 楼 hanjun0612 的回复:
public static s[] t=new s[4]

t[0]=new s();
test.t[0].str="Hello";

多谢多谢。
Rocco_Cui 2018-08-07
  • 打赏
  • 举报
回复
引用 1 楼 wanghui0380 的回复:
public static s[] t=new s[4]

你只是new了一个数组,但是数组里的元素是null,s为引用类型,不是值类型,所以他默认值是null


谢谢。谢谢。。。醍醐灌顶。。
晨易夕 2018-08-07
  • 打赏
  • 举报
回复
看到这个我另外想起了个问题,就是结构体中存在一个类成员,这个类未初始化的时候却已经指向了一个内存地址,这个有大佬知道吗。
正怒月神 2018-08-07
  • 打赏
  • 举报
回复
public static s[] t=new s[4]

t[0]=new s();
test.t[0].str="Hello";
wanghui0380 2018-08-07
  • 打赏
  • 举报
回复
public static s[] t=new s[4]

你只是new了一个数组,但是数组里的元素是null,s为引用类型,不是值类型,所以他默认值是null

110,534

社区成员

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

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

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