类里面嵌套了一个struct类型
这是一个类里嵌套了一个struct 请问我标红的代码段struct是存放在堆里的还是存放在栈里的?我分析它应该是在栈里的 否则下面Main函数里不会报错。这段代码来自http://blog.csdn.net/onlyou930/article/details/5568319 只是我稍做了修改 把struct放到了类里面而已
代码如下:
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
public class Draw
{
public struct Rectangle
{
private int _Width;
private int _Height;
public int Width
{
get { return _Width; }
set { _Width = value; }
}
public int Height
{
get { return _Height; }
set { _Height = value; }
}
public Rectangle(int width, int height)
{
this._Width = width;
this._Height = height;
}
}
private Rectangle _rect = new Rectangle(0, 0);
public Rectangle MyRect
{
get { return _rect; }
set { _rect = value; }
}
public Draw() { }
public Draw(int mapWidth, int mapHeight)
{
_rect = new Rectangle(mapWidth, mapHeight);
}
}
class Program
{
static void Main(string[] args)
{
Draw draw = new Draw();
draw.MyRect.Width = 20;//《== 编译错误
draw.MyRect.Height = 12;//《== 编译错误
}
}
}