2-dim struct table
In C, I can use the following form inside a method.
void foo() {
struct {
int val;
int color;
string desc;
} ItemMap[] = {
开发者_高级运维 { 1, 2, "test"},
{ 2, 3, "test"},
}
// process tasks according to ItemMap.
}
If I want to do the same thing under C#, how to achieve it ?
You could use anonymous types for that in C#:
var ItemMap = new[] { new { val = 1, color = 2, desc = "test" },
new { val = 2, color = 3 , desc = "test" } };
string description = ItemMap[0].desc;
This would however create a one-dimensional array of an anonymous class for you not a struct - also it is read only. If you specifically need a struct / value type that is mutable, you will have to declare the struct outside of your method.
精彩评论