How to Change an Object's Mesh to another Object's Mesh via Code C#

Hi everyone, Is there a way to change an object’s mesh to another object’s mesh through code? Say like I have a square and a cylinder how would I be able to change the square’s mesh to the cylinder’s mesh?

object1.GetComponent().sharedMesh = object2.GetComponent().sharedMesh;