【发布时间】:2014-10-28 09:14:30
【问题描述】:
为什么下面的代码不起作用?
如果我将h 从属性更改为字段,它会起作用!
或者,如果我将 FileHeader 从 struct 更改为 class,它会起作用!
我只是在寻找为什么它不起作用的答案。
public class MyFile
{
public struct FileHeader
{
public List<string> ColNames
{
get;
set;
}
public void setColNames()
{
ColNames = new List<string>();
ColNames.Add("address");
}
}
public FileHeader h
{
get;
set;
}
}
public class Program
{
static void Main(string[] args)
{
MyFile o = new MyFile();
o.h.setColNames();
Console.WriteLine(o.h.ColNames[0]); // <- Doesn't work! No elements
string line = System.Console.ReadLine();
}
}
【问题讨论】:
-
可变结构是邪恶的。现在您知道原因之一了。
-
"...不起作用"; “... 有用!” - 这对帮助我们帮助您并没有真正的帮助。你是什么意思不起作用?编译时错误?运行时错误?结果不正确?我们可以猜测,因为结构有一些特定的特征可以很容易地找出你在抱怨什么。但说真的……帮帮我们吧。
标签: c# properties struct