C# Container how to add int variable?

Hi guys.

Still new to C#. I do now know how to create a container and add components to it. Is there a way to add a integer to a container?

For example, I have:

Component Item1 = new Component();

inventory inventoryObject = new inventory();


This does add a component (Item1) successfully. Is there a way to add a integer though? Thank you in advance.


Oh yes, above all that other code I have:

public class inventory


public Container iContainer = new Container();


2 Answers

  • 3 years ago

    A Container is a very specialized collection type that can only hold objects that implement the IComponent interface. It would not make sense to put an integer into a Container, depending on exactly what you are trying to do you should probably be using a different type of collections, for example List<int> list = new List<int>(); will create a collection that can hold integers.

  • AJ
    Lv 7
    3 years ago

    You are trying to add a class to a class. That makes no sense.

    Use List<type> as the container of your component

    List<inventory> InventoryList = new List<inventory>;


    In order to add an integer, add it to the compenent class.

Still have questions? Get your answers by asking now.