You're right, in a perfect world codesharing would not be needed or tolerated. However, codesharing actually helps in many cases. For instance, Great Lakes Aviation codes shares with United and Frontier from Denver to many small cities across the Midwest, such as Hays, Great Bend, Dodge City, and Liberal, KS
; North Platte, Scottsbluff, and Chadron, NE; Vernal and Moab, UT; and tons of towns and cities across Colorado and Wyoming, as well as others in AZ
, ND, SD
, and TX
. In this case, codesharing allows customers seamless access from major cities across the world to these smaller cities. The same goes for many of the US regional airlines, including American Eagle's carriers (before AMR completely owned them), Delta Connection carriers, United Express carriers, etc. Many of these smaller cities cannot be served economically by more than one airline, or even just one airline, which is why there are EAS subsidies.
In terms of international flight, it is difficult for airlines to start service between countries. Various bilateral agreements must be negotiated, and then the governments decide which airlines receive rights to fly to the other country. Also, even if, for example, United States carriers recieve rights to fly to Paris, they do not have the right to fly their own planes inside of France or the rest of the European Union. Codesharing and interline ticketing reduces the hassle the customer must endure by allowing him or her to enjoy a seamless travel experience.
As far as codesharing domestically among large airlines, while I am also against it, it can sometimes be necessary and helpful. For instance, if one carrier had a major presence in the East and Midwest but had very little presence in the West, it would make since for them to codeshare with that airline as the startup costs for a completely new operation in a different part of the country are extraordinarily high. Since, most likely, the Eastern carrier would not be entering the Western market because of the high start up costs, they are not really stealing competition from each other. All they are really doing is providing a seamless travel system for the passenger.
Hope this helps