Good day. I start to study Unity and, as a result, I work with C #. Please tell me the equivalent of ArrayList <> in Java in C #. It is required to store an array of buttons. Tried to use List<Button> from System.Collections.Generic; , everything seems to be fine, but I don’t know how to get the item by the index. In Java, I would use .get() , and how it is not clear. Docks read, did not find analog of .get() .

    2 answers 2

    To refer to a collection item, use the index [i] property:

     List<Button> buttons = ...; Button firstButton = buttons[0]; 
    • Thank! But do not tell the analogue .SetActive() in the UI Button for UI Image ? The image is required to be made visible / invisible. - Pollux
    • @Igor, There is no such function for UI Image. - Pollux
    • In principle, it is suitable to use .CrossFadeAlpha(0, 0, false); - Pollux

    As Dmitry D. has already written. Reversing by index through parentheses, you can also, for example, search for a necessary element without going through the entire array, or the list. As an example, I will show a search in the button contents. those. by its content.

     IList<Button> buttonArray = new List<Button> { new Button{Content="FirstButton"}, new Button{Content="AnotherButton"} } var neededBtn = buttonArray?.TryFind( a => a.Content.Equals( "FirstButton" ) ); 

    Now if we look at the contents of neededBtn then there will be our button with the content of FirstButton

    • 1. IList <> does not have TryFind. 2. "without going through the whole array" - this is exactly what you are doing - hazzik