My custom type has many different properties with most of them being optional, how I can make it more compact

I’m developing a tool for modding a game and I want to remake custom type as C# class. This type has 1 required property and many other optional ones. I’m getting all of the data for custom class from XML file, so is there any way I can make this constructor better?

    public class Item(
         string identifier,
         string? nameIdentifier = null,
         string? fallbackNameIdentifier = null,
         string? descriptionIdentifier = null,
         string? name = null,
         string? aliases = null,
         string? tags = null,
         Item.Categories? category = null,
         bool? allowAsExtraCargo = null,
         float? interactDistance = null,
         float? interactPriority = null,
         bool? interactThroughWalls = null,
         bool? hideConditionBar = null,
         bool? hideConditionInTooltip = null,
         bool? requireBodyInsideTrigger = null,
         bool? requireCursorInsideTrigger = null,
         bool? requireCampaignInteract = null,
         bool? focusOnSelected = null,
         float? offsetOnSelected = null,
         float? health = null,
         bool? allowSellWhenBroken = null,
         bool? indestructible = null,
         bool? damagedByExplosions = null,
         float? explosionDamageMultiplier = null,
         bool? damagedByProjectiles = null,
         bool? damagedByMeleeWeapons = null,
         bool? damagedByRepairTools = null,
         bool? damagedByMonsters = null,
         bool? fireProof = null,
         bool? waterProof = null,
         float? impactTolerance = null,
         float? onDamagedThreshold = null,
         float? sonarSize = null,
         bool? useInHealthInterface = null,
         bool? disableItemUsageWhenSelected = null,
         string? cargoContainerIdentifier = null,
         bool? useContainedSpriteColor = null,
         bool? useContainedInventoryIconColor = null,
         float? addedRepairSpeedMultiplier = null,
         float? addedPickingSpeedMultiplier = null,
         bool? cannotRepairFail = null,
         string? equipConfirmationText = null,
         bool? allowRotatingInEditor = null,
         bool? showContentsInTooltip = null,
         bool? canFlipX = null,
         bool? canFlipY = null,
         bool? isDangerous = null,
         int? maxStackSize = null,
         bool? allowDroppingOnSwap = null,
         bool? resizeHorizontal = null,
         bool? resizeVertical = null,
         string? description = null,
         string? allowedUpgrades = null,
         bool? hideInMenus = null,
         string? subcategory = null,
         bool? linkable = null,
         string? spriteColor = null,
         float? scale = null)
    {
        public enum Categories
        {
            Decorative,
            Machine,
            Medical,
            Weapon,
            Diving,
            Equipment,
            Fuel,
            Electrical,
            Material,
            Alien,
            Wrecked,
            Misc
        }

        // All of Item attributes
        public required string identifier = identifier;
        public string? nameIdentifier = nameIdentifier;
        public string? fallbackNameIdentifier = fallbackNameIdentifier;
        public string? descriptionIdentifier = descriptionIdentifier;

        public string? name = name;
        public string? aliases = aliases;
        public string? tags = tags;
        public Categories? category = category;

        public bool? allowAsExtraCargo = allowAsExtraCargo;
        public float? interactDistance = interactDistance;
        public float? interactPriority = interactPriority;
        public bool? interactThroughWalls = interactThroughWalls;

        public bool? hideConditionBar = hideConditionBar;
        public bool? hideConditionInTooltip = hideConditionInTooltip;
        public bool? requireBodyInsideTrigger = requireBodyInsideTrigger;
        public bool? requireCursorInsideTrigger = requireCursorInsideTrigger;

        public bool? requireCampaignInteract = requireCampaignInteract;
        public bool? focusOnSelected = focusOnSelected;
        public float? offsetOnSelected = offsetOnSelected;
        public float? health = health;

        public bool? allowSellWhenBroken = allowSellWhenBroken;
        public bool? indestructible = indestructible;
        public bool? damagedByExplosions = damagedByExplosions;
        public float? explosionDamageMultiplier = explosionDamageMultiplier;

        public bool? damagedByProjectiles = damagedByProjectiles;
        public bool? damagedByMeleeWeapons = damagedByMeleeWeapons;
        public bool? damagedByRepairTools = damagedByRepairTools;
        public bool? damagedByMonsters = damagedByMonsters;

        public bool? fireProof = fireProof;
        public bool? waterProof = waterProof;
        public float? impactTolerance = impactTolerance;
        public float? onDamagedThreshold = onDamagedThreshold;

        public float? sonarSize = sonarSize;
        public bool? useInHealthInterface = useInHealthInterface;
        public bool? disableItemUsageWhenSelected = disableItemUsageWhenSelected;
        public string? cargoContainerIdentifier = cargoContainerIdentifier;

        public bool? useContainedSpriteColor = useContainedSpriteColor;
        public bool? useContainedInventoryIconColor = useContainedInventoryIconColor;
        public float? addedRepairSpeedMultiplier = addedRepairSpeedMultiplier;
        public float? addedPickingSpeedMultiplier = addedPickingSpeedMultiplier;

        public bool? cannotRepairFail = cannotRepairFail;
        public string? equipConfirmationText = equipConfirmationText;
        public bool? allowRotatingInEditor = allowRotatingInEditor;
        public bool? showContentsInTooltip = showContentsInTooltip;

        public bool? canFlipX = canFlipX;
        public bool? canFlipY = canFlipY;
        public bool? isDangerous = isDangerous;
        public int? maxStackSize = maxStackSize;

        public bool? allowDroppingOnSwap = allowDroppingOnSwap;
        public bool? resizeHorizontal = resizeHorizontal;
        public bool? resizeVertical = resizeVertical;
        public string? description = description;

        public string? allowedUpgrades = allowedUpgrades;
        public bool? hideInMenus = hideInMenus;
        public string? subcategory = subcategory;
        public bool? linkable = linkable;

        public string? spriteColor = spriteColor;
        public float? scale = scale;

        public List<XmlElement>? children = null;

        /// <summary>
        /// Gets this Item represent
        /// </summary>
        /// <returns>XmlElement represent of the Item</returns>
        public XmlElement GetAsXml()
        {
            XmlDocument xmlDoc = new();
            XmlElement newItem = xmlDoc.CreateElement("Item");

            foreach(var attr in this.GetType().GetProperties())
            {
                if (attr.GetValue(this, null) != null)
                {
                    newItem.SetAttribute(attr.Name.ToLower(), value: attr.GetValue(this, null).ToString());
                }
            }

            newItem.InnerText = "";

            if (this.children != null)
            {
                foreach (var child in this.children)
                {
                    newItem.AppendChild(child);
                }
            }

            return newItem;
        }
    }
}

I have thought of 2 ways this can potentially be done, passing properties either as a dictionary or a new custom type containing all of available properties, but I’m not sure if they would be any better.

New contributor

Planeptunia is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

2

How about this, define a class to hold all your optional properties:

public class ItemData
{
     public string? nameIdentifier { get; set; } = null;
     public string? fallbackNameIdentifier { get; set; } = null;
     public string? descriptionIdentifier { get; set; } = null;
     public string? name { get; set; } = null;
     public string? aliases { get; set; } = null;
     public string? tags { get; set; } = null;
     public ItemData.Categories? category { get; set; } = null;
     public bool? allowAsExtraCargo { get; set; } = null;
     public float? interactDistance { get; set; } = null;
     public float? interactPriority { get; set; } = null;
     public bool? interactThroughWalls { get; set; } = null;
     public bool? hideConditionBar { get; set; } = null;
     public bool? hideConditionInTooltip { get; set; } = null;
     public bool? requireBodyInsideTrigger { get; set; } = null;
     public bool? requireCursorInsideTrigger { get; set; } = null;
     public bool? requireCampaignInteract { get; set; } = null;
     public bool? focusOnSelected { get; set; } = null;
     public float? offsetOnSelected { get; set; } = null;
     public float? health { get; set; } = null;
     public bool? allowSellWhenBroken { get; set; } = null;
     public bool? indestructible { get; set; } = null;
     public bool? damagedByExplosions { get; set; } = null;
     public float? explosionDamageMultiplier { get; set; } = null;
     public bool? damagedByProjectiles { get; set; } = null;
     public bool? damagedByMeleeWeapons { get; set; } = null;
     public bool? damagedByRepairTools { get; set; } = null;
     public bool? damagedByMonsters { get; set; } = null;
     public bool? fireProof { get; set; } = null;
     public bool? waterProof { get; set; } = null;
     public float? impactTolerance { get; set; } = null;
     public float? onDamagedThreshold { get; set; } = null;
     public float? sonarSize { get; set; } = null;
     public bool? useInHealthInterface { get; set; } = null;
     public bool? disableItemUsageWhenSelected { get; set; } = null;
     public string? cargoContainerIdentifier { get; set; } = null;
     public bool? useContainedSpriteColor { get; set; } = null;
     public bool? useContainedInventoryIconColor { get; set; } = null;
     public float? addedRepairSpeedMultiplier { get; set; } = null;
     public float? addedPickingSpeedMultiplier { get; set; } = null;
     public bool? cannotRepairFail { get; set; } = null;
     public string? equipConfirmationText { get; set; } = null;
     public bool? allowRotatingInEditor { get; set; } = null;
     public bool? showContentsInTooltip { get; set; } = null;
     public bool? canFlipX { get; set; } = null;
     public bool? canFlipY { get; set; } = null;
     public bool? isDangerous { get; set; } = null;
     public int? maxStackSize { get; set; } = null;
     public bool? allowDroppingOnSwap { get; set; } = null;
     public bool? resizeHorizontal { get; set; } = null;
     public bool? resizeVertical { get; set; } = null;
     public string? description { get; set; } = null;
     public string? allowedUpgrades { get; set; } = null;
     public bool? hideInMenus { get; set; } = null;
     public string? subcategory { get; set; } = null;
     public bool? linkable { get; set; } = null;
     public string? spriteColor { get; set; } = null;
     public float? scale { get; set; } = null;

    public enum Categories
    {
        Decorative,
        Machine,
        Medical,
        Weapon,
        Diving,
        Equipment,
        Fuel,
        Electrical,
        Material,
        Alien,
        Wrecked,
        Misc
    }
}

And pass the ItemData class to the Item constructor:

 public class Item
 {
     public string Identifier { get; private set; }
     public ItemData OptionalData { get; private set; }
     public List<XmlElement>? Children { get; private set; } = null;
     public Item(string identifier, ItemData optionalData)
     {
         Identifier = identifier;
         OptionalData = optionalData;

         // Get the name
         string? name = OptionalData.name;
     }
 }

You will also have to update your Item.GetAsXml() method to correctly serialize your data:

public XmlElement GetAsXml()
{
    XmlDocument xmlDoc = new();
    XmlElement newItem = xmlDoc.CreateElement("Item");

    // Identifier
    PropertyInfo identInfo = this.GetType().GetProperty(nameof(Identifier));
    newItem.SetAttribute(identInfo.Name.ToLower(), value: identInfo.GetValue(this, null).ToString());

    // Optional data attributes
    foreach (var attr in OptionalData.GetType().GetProperties())
    {
        if (attr.GetValue(OptionalData, null) != null)
        {
            newItem.SetAttribute(attr.Name.ToLower(), value: attr.GetValue(OptionalData, null).ToString());
        }
    }

    newItem.InnerText = "";

    if (this.Children != null)
    {
        foreach (var child in this.Children)
        {
            newItem.AppendChild(child);
        }
    }

    return newItem;
}

You can then create items like:

 ItemData itemData = new ItemData();
 itemData.name = "name1";
 itemData.health = 99;
 Item item = new Item("identifier1", itemData);

When you serialize the above item:

 XmlElement xml = item.GetAsXml();
 Debug.WriteLine(xml.OuterXml);

You get this output:

<Item identifier="identifier1" name="name1" health="99"></Item>

New contributor

BigBoss is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật