Search code examples
c#unity-game-enginejetbrains-iderider

JetBrains Rider suggests "make parameter type Component". Why?


I am currently using JetBrains Rider as the IDE.

private void RemoveAlert(int idx, Image lockImg)
{
    ...
}

While writing the code above, I received a suggestion from Rider to convert the Image type parameter of the RemoveAlert method to a Component. I do not know why.

I wonder if this proposal is more efficient or customary. If you know anything, please answer.

Screenshot for the suggestion from Rider:


Solution

  • I can't see exactly how you're using the variable (the hover-text is covering some stuff) but ReSharper will suggest this when you're using a derived class but not any of its extra functionality. It looks like you're getting a reference to its gameObject property, and if you look at the inheritance hierarchy, Component is the lowest-level class that has that property:

    UnityEngine.UI.Image
    UnityEngine.UI.MaskableGraphic
    UnityEngine.UI.Graphic
    UnityEngine.EventSystems.UIBehaviour 
    UnityEngine.MonoBehaviour
    UnityEngine.Behaviour
    UnityEngine.Component <- has gameObject property
    UnityEngine.Object    <- doesn't have gameObject property
    

    Also, if you find this annoying, you can put the following over the function:

    // ReSharper disable once SuggestBaseTypeForParameter
    

    Or you can put this at the top of the file itself to cover all its functions:

    // ReSharper disable SuggestBaseTypeForParameter