How to change image

How would I change the image inside of a Image component through C# code?

Ok so, basic knowledge first, when importing an Image (called Sprite) into Unity you have got to select, while inspecting it, the “2D and UI” option of the dropdown at the top.
Also you have got to import UnityEngine.UI in order to execute the following code and change the UI image.

 public Image image;
 public Sprite sprite1;
 public Sprite sprite2;
 
 public void ChangeSprite(){
      if (image.sprite == sprite1)
           image.sprite = sprite2;
      else 
           image.sprite = sprite1;
 }

Thanks! It worked!