在字符串数组中初始化新字符串

时间:2012-04-21 20:55:23

标签: c# .net initialization

我一直试图找出是否有更好的方法来初始化我在此代码段中使用的字符串数组。 我想知道是否有一个函数,或者可能是new的一些使用,这将使代码中的所有空字符串的调用和赋值变得不必要。所以我可以在创建数组的同时初始化它们。

                    foreach (var unit in unitList)
                    {
                        //Sort units by each army
                        string unitName = unit.UnitName;
                        armyUnits.Add(unitName, unit);

                        //Sort unit properties by unit
                        List<string> properites = new List<string>();

                        string composition        ="";
                        string weaponSkill        ="";
                        string ballisticSkill     ="";
                        string strength           ="";
                        string initiative         ="";
                        string toughness          ="";
                        string wounds             ="";
                        string attacks            ="";
                        string leadership         ="";
                        string savingThrow        ="";
                        string specialRules       ="";
                        string dedicatedTransport ="";
                        string options            ="";
                        string armour             ="";
                        string weapons            ="";


                        properites.AddRange(new string[15]{

                        composition            = unit.Composition,
                        weaponSkill            = unit.WeaponSkill,
                        ballisticSkill         = unit.BallisticSkill,
                        strength               = unit.Strength,
                        initiative             = unit.Initiative,
                        toughness              = unit.Toughness,
                        wounds                 = unit.Wounds,
                        attacks                = unit.Attacks,
                        leadership             = unit.Leadership,
                        savingThrow            = unit.SaveThrow,
                        specialRules           = unit.SpecialRules,
                        dedicatedTransport     = unit.DedicatedTransport,
                        options                = unit.Options,
                        armour                 = unit.Armour,
                        weapons                = unit.Weapons
                        });


                    }

编辑: 所以看起来你可以在数组中做new String(unit.Composition.ToCharArray())。我认为这不是更易读或更快。

  properites.AddRange(new string[1]{
  new String(unit.Composition.ToCharArray())}

2 个答案:

答案 0 :(得分:4)

                    foreach (var unit in unitList)
                    {
                        //Sort units by each army
                        string unitName = unit.UnitName;
                        armyUnits.Add(unitName, unit);

                        //Sort unit properties by unit
                        List<string> properites = new List<string>();

                        properites.AddRange(new string[15]{

                        unit.Composition,
                        unit.WeaponSkill,
                        unit.BallisticSkill,
                        unit.Strength,
                        unit.Initiative,
                        unit.Toughness,
                        unit.Wounds,
                        unit.Attacks,
                        unit.Leadership,
                        unit.SaveThrow,
                        unit.SpecialRules,
                        unit.DedicatedTransport,
                        unit.Options,
                        unit.Armour,
                        unit.Weapons
                        });


                    }

根本不需要变量。实际上,你不需要列表!

                    var properties = new [] {
                        unit.Composition,
                        unit.WeaponSkill,
                        unit.BallisticSkill,
                        unit.Strength,
                        unit.Initiative,
                        unit.Toughness,
                        unit.Wounds,
                        unit.Attacks,
                        unit.Leadership,
                        unit.SaveThrow,
                        unit.SpecialRules,
                        unit.DedicatedTransport,
                        unit.Options,
                        unit.Armour,
                        unit.Weapons,
                    }

列表支持添加和删除此处似乎不使用的项目。您也不需要15的数组计数,因为编译器会将其计算出来。出于同样的原因,您也不需要数组类型。 C#是一种非常简洁的语言!

我还在“unit.Weapons”之后添加了一个最后一个逗号,以使所有列表项对称。这样编译得很好,使复制,粘贴和重新排序更简单。

答案 1 :(得分:3)

您也不需要使用AddRange方法。您只需使用集合初始化语法。

//Sort unit properties by unit
var properites = new List<string>
                     {
                         unit.Composition,
                         unit.WeaponSkill,
                         unit.BallisticSkill,
                         unit.Strength,
                         unit.Initiative,
                         unit.Toughness,
                         unit.Wounds,
                         unit.Attacks,
                         unit.Leadership,
                         unit.SaveThrow,
                         unit.SpecialRules,
                         unit.DedicatedTransport,
                         unit.Options,
                         unit.Armour,
                         unit.Weapons
                     };