Simply stick it in a method with a useful name:
public void SetColorSwitchRedState(int id, int newState)
{
GameObject.Find("colorswitchred (" + id.ToString() + ")").transform.GetComponent<Main>().state = newState;
}
Then you can call that:
SetColorSwitchRedState(42, 0);