How do I change one value of a vector?

If I’m not mistaken, I’ve used code like this before:

transform.position.z = <i>[value]</i>; //but now it doesn't work.

Instead, it gives the following error:
error CS1612: Cannot modify a value type return value of ‘UnityEngine.Transform.position’. Consider storing the value in a temporary variable.

transform.position[3] = <i>[value]</i>; //doesn't work either.

I thought I found a way to get it to work, but it’s incredibly longwinded, redundant, and ugly:

transform.position.Set(transform.position.x, transform.position.y, <i>[value]</i>); /*Gross, and edit: still doesn't work.*/

Is not being able to do the earlier ones a bug? Is there any better workaround?

(Making a temporary variable isn’t fun - my code’s now 21 lines instead of 9.)

you need to make a temporary variable first.

Vector3 position = transform.position;
position[2] = [value]; // the Z value
transform.position = position;

Basically it is the same as the ideas before. You can create a static class in c#.
You can use shorter class and method names as well. ChangeX to X etc.

public static class Utils {

    public static Vector3 ChangeX(Vector3 v, float x)
        return new Vector3(x, v.y, v.z);

    public static Vector3 ChangeY(Vector3 v, float y)
        return new Vector3(v.x, y, v.z);

    public static Vector3 ChangeZ(Vector3 v, float z)
        return new Vector3(v.x, v.y, z);


Usage in a different class.

public class Something: MonoBehaviour
 void Update() {
     transform.position = Utils.ChangeX(transform.position, 1); // change x