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

本文关键字:字符串 数组 初始化 | 更新日期: 2023-09-27 18:32:02

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

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

                    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#是一种非常简洁的语言!

我还在"单位"之后添加了最后一个逗号。武器",使所有列表项对称。这编译得很好,使复制、粘贴和重新排序更简单。

也不需要使用 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
                     };