Using electrochemistry

You have seen that iron is better protected against corrosion by a metal which is a member of a couple having a more negative electrode potential than iron.

Magnesium is another example of a metal which prevents or greatly reduces corrosion of ships hulls or bridge pylons due to reaction of the iron contained in these with dissolved oxygen.
 
O2(g) + 4H+ + 4e 2H2O

AND
Mg Mg2+ + 2e
rather than
Fe Fe2+ + 2e

As the magnesium or zinc electrode is destroyed in the process, it is called a sacrificial anode.