C#中如何初始化结构体中类变量

如题:

public struct cylinder
{
public
int height;
public
Circle underside;
public cylinder(int n)
{
height = n;
underside = new Circle();
underside.diameter = 10;
}
}

class Program
{
static void Main(string[] args)
{
cylinder myCylinder;
myCylinder.height = 10;

Console.WriteLine("The Cylinder's height={0},usderside={1}",myCylinder.height,myCylinder.underside.diameter*3.1415926);
//使用了可能未赋值的字段“underside
Console.ReadKey();
}
}
...全文
244 2 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
引用 1 楼 bdmh 的回复:
myCylinder.underside = new Circle();
嗯,这样确实可以。。。 想问下,为什么在构造函数中初始化不行呢?
bdmh 2013-05-28
  • 打赏
  • 举报
回复
myCylinder.underside = new Circle();

111,097

社区成员

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

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

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