C#中内嵌String []数组条件 [英] C# inline conditional in string[] array

查看:99
本文介绍了C#中内嵌String []数组条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你怎么能做到以下为C#中的String []数组内联的条件。基于参数,我想包括一组字符串...还是不行。这个问题的<一后随href=\"http://stackoverflow.com/questions/7788270/assigning-values-to-c-sharp-string-array-based-on-parameter\"标题=分配值基于参数C#字符串数组>这一项上计算器。

  //不能编译
        布尔msettingvalue = FALSE;
        字符串[]设置;
        如果(msettingvalue)
            设置=新的String [] {
                设置1,1,
                设置2,苹果,
                ((msettingvalue == true)而msetting,真正的:)}?;

如果msettingvalue是真的,我想有两个字符串msetting,真。否则没有字符串

EDIT1
这并不一定是一个键值对...如果它是5串是(或不被)加入...我不认为这会是棘手的。

(也可以......有人用足够的代表处做一个直列条件或有条件的内联的标签?)


解决方案

 设置=新的String [] {设置1,1,设置2,苹果}
    .Concat(msettingvalue新的字符串[] {msetting,真正的}:新的字符串[0]);
    .ToArray()

How could you do the following inline conditional for a string[] array in C#. Based on a parameter, I'd like to include a set of strings...or not. This question is a followup of this one on stackoverflow.

        //Does not compile
        bool msettingvalue=false;
        string[] settings;
        if(msettingvalue)
            settings = new string[]{
                "setting1","1",
                "setting2","apple",
                ((msettingvalue==true) ? "msetting","true" :)};

If msettingvalue is true, I'd like to include two strings "msetting","true" : otherwise no strings.

Edit1 It doesn't have to be a key value pair...what if it were 5 strings to be (or not to be) added...I didn't think it'd be that tricky.

(Also...could someone with enough rep make a "inline-conditional" or "conditional-inline" tag?)

解决方案

settings = new string[]{"setting1","1", "setting2","apple"}
    .Concat(msettingvalue ? new string[] {"msetting","true"} : new string[0]);
    .ToArray()

这篇关于C#中内嵌String []数组条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆