What is the correct syntax to create a new object with a RectTransform from script?

What is the syntax for creating a new GameObject that has a RectTransform instead of a Transform in C#? I thought I could do this, but no:

RectTransform myObject = (RectTransform) new GameObject("myObject").transform;

I found that this works:

GameObject myGO = new GameObject("MyGO", typeof(RectTransform));

Haha, I was really, really, really hoping Unity would understand what I meant by that, and, fortunately, it seems it did :).

When you call “new GameObject”, one of the overloads allows you to specify a set of Components that will be immediately added to the GameObject when it’s created. Fortunately, it seems that adding “typeof(RectTransform)” here will correctly create the GameObject with a RectTransform out of the gate.

You can get it by

RectTransform rect = (RectTransform)this.gameObject.transform; //Casting to RectTransform

RectTransform rect = this.gameObject.GetComponent<RectTransform>(); //Using GetComponent