像PHP这样的c#数组

时间:2017-01-08 05:01:41

标签: c# arrays multidimensional-array

我想在C#中设置下一个代码

'price_changes' => array(
    'size' => array(
        'Large'  => '+1',
        'Medium'   => '-3',
    ),
),

我如何在C#(Winforms)中执行此操作

或者像这样:

if "dylan" in "bob dylan":  # True

if "bob dylan".startswith("dylan"):  # False

if "dylan" in "Bob Dylan":  # False

if "dylan".lower() in "Bob Dylan".lower():  # True

很多人都帮忙。

2 个答案:

答案 0 :(得分:3)

除了Enigmativity发布的答案更像是PHP代码之外,下面的代码显示了使用object-properties来实现类似的结构,这可能比在某些情况下使用嵌套的Dictionary<string, Dictionary<string, string ...更简单:

var price_changes = new { 
    color = new { Red = "2", Blue = "-10%" }, 
    size = new { Large = 1, Medium = -3 } 
};

使用非常简单:

var x = price_changes.size.Large;

答案 1 :(得分:1)

这将是C#等价物:

var data = new Dictionary<string, Dictionary<string, Dictionary<string, string>>>()
{
    {
        "price_changes", new Dictionary<string, Dictionary<string, string>>()
        {
            {
                "color", new Dictionary<string, string>()
                {
                    { "Red", "2" },
                    { "Blue", "-10%" },
                }
            },
            {
                "size", new Dictionary<string, string>()
                {
                    { "Large", "+1" },
                    { "Medium", "-3" },
                }
            },
        }
    },
};

如果你写了var blue = data["price_changes"]["color"]["Blue"];,变量blue将包含"-10%"