How do you change a sprite's sorting layer in C#?

Basically I am making a 2D platformer and I have a sprite that is in the foreground and when my player flips a switch, I want the sprite to move to the background. I can do this manually by changing the Sprite Renderer sorting layer, but I can’t seem to figure out how to change this in the code, or if it is even possible?

If not, what would be an alternate solution for what I’m trying to do?

To add the layer changing to emalbs answer since his example only changed sorting order on the same layer

using UnityEngine;
    using System.Collections;


    public class SortingOrderScript : MonoBehaviour
    {
        public const string LAYER_NAME = "TopLayer";
        public int sortingOrder = 0;
        private SpriteRenderer sprite;

        void Start()
        {
           sprite = GetComponent<SpriteRenderer>();

           if (sprite)
           {
             sprite.sortingOrder = sortingOrder;
             sprite.sortingLayerName = LAYER_NAME;
           }
        }
    }

Have a go at setting your SpriteRenderer’s sortingOrder. The following example should enable you to manipulate it in the Inspector as your game runs.

using UnityEngine;
using System.Collections;

public class SortingOrderScript : MonoBehaviour
{
	public int sortingOrder = 0;
	private SpriteRenderer sprite;

	void Start()
	{
		sprite = GetComponent<SpriteRenderer>();
	}

	void Update()
	{
		if (sprite)
			sprite.sortingOrder = sortingOrder;
	}
}

thank you, NoseKills - I used this, too!

Thank you very much !!
I was looking for it a lot !!

Heyy guys what about multiple sprites at the same time?

using UnityEngine;
using System.Collections;

 public class SortingOrderScript: MonoBehaviour
 {
   //  public const string LAYER_NAME = "TopLayer";
     public int sortingOrder = 1;
     private SpriteRenderer sprite;

     void Start()
     {
        sprite = GetComponent<SpriteRenderer>();

        if (sprite)
        {
          sprite.sortingOrder = sortingOrder;
          sprite.sortingLayerName = LAYER_NAME;
        }
     }
 }

// This will work for me. So I share after changes in current suggestions.
Hope it will help someone else as well.