Fastest way to set z axis rotation to 0 C#

In the script I’m making I need to set the rotation of the z axis to 0 while preserving the rotations of the other axis when I press space. What is the simplest way to do that?

not tested but should work

    Vector3 eulerRotation = transform.rotation.eulerAngles;
    transform.rotation = Quaternion.Euler(eulerRotation.x, eulerRotation.y, 0);