2

我一直在尝试找出是否有更好的方法来初始化我在此代码片段中使用的字符串数组。我想知道是否有一个函数,或者也许使用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())}
4

2 回答 2

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”之后添加了一个逗号,以使所有列表项对称。这编译得很好,使复制、粘贴和重新排序更简单。

于 2012-04-21T20:58:35.273 回答
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
                     };
于 2012-04-22T01:09:49.927 回答