Home

How C# Supports Object-Oriented Concepts

|
Updated:  
2018-01-30 12:38:50
|
C# 2010 All-in-One For Dummies
Explore Book
Buy On Amazon
Okay, how does C# implement object-oriented programming? In a sense, this is the wrong question. C# is an object-oriented language; however, it doesn’t implement object-oriented programming — the programmer does. You can certainly write a non-object-oriented program in C# or any other language (by, for instance, writing all of Microsoft Word in Main()). Something like “you can lead a horse to water” comes to mind. But you can easily write an object-oriented program in C#. These C# features are necessary for writing object-oriented programs:
  • Controlled access: C# controls the way in which class members can be accessed. C# keywords enable you to declare some members wide open to the public, whereas internal members are protected from view and some secrets are kept private. Notice the little hints.
  • Specialization: C# supports specialization through a mechanism known as class inheritance. One class inherits the members of another class. For example, you can create a Car class as a particular type of Vehicle.
  • Polymorphism: This feature enables an object to perform an operation the way it wants to. The Rocket type of Vehicle may implement the Start operation much differently from the way the Car type of Vehicle does. But all Vehicles have a Start operation, and you can rely on that.
  • Indirection. Objects frequently use the services of other objects — by calling their public methods. But classes can “know too much” about the classes they use. The two classes are then said to be “too tightly coupled,” which makes the using class too dependent on the used class. The design is too brittle — liable to break if you make changes. But change is inevitable in software, so you should find more indirect ways to connect the two classes. That's where the C# interface construct comes in.

About This Article

This article is from the book: 

About the book author:

John Paul Mueller is a freelance author and technical editor. He has writing in his blood, having produced 100 books and more than 600 articles to date. The topics range from networking to home security and from database management to heads-down programming. John has provided technical services to both Data Based Advisor and Coast Compute magazines.

Bill Sempf is a seasoned programmer and .NET evangelist specializing in .NET applications.

Chuck Sphar is a programmer and former senior technical writer for the Visual C++ product group at Microsoft.