Okay, let’s try a little analogy first before we get into the “Magic Menu” C# code to refresh our understanding of object-oriented programming!
A car is an object.
The car has a color – red, beige, purple – whatever you like. “color” is a property of our car object.
Say you don’t like the color anymore of that car, so you decide to get it painted. “Paint car” could be thought of as an action on the car, or a method. We might pass a parameter to “Paint car” that indicates to paint the whole car, maybe “all car”. That “Paint car” method will change the value of the “color” property (and paint our whole car).
Now say the car is parked somewhere and gets scratched. “Car gets scratched” would make you want to fix the scratch yes? This trigger is called an event. So in the event of “Car gets scratched”, we can call another method, “Fix scratch”. Since “Fix scratch” is being called on the occurrence of an event, this method can be called an event handler. “Fix scratch” will probably look for the value of the “color” property so we know what paint to buy. We could call our “Paint car” method again within “Fix scratch”, but pass a parameter “just the scratch”. “Paint car” will paint over our scratch, but not bother changing the value of the “color” property.
We’ll wrap together these properties, methods and events, which we call members, into a section of C# code called a class. Think of the class as your all-in-one manufacturing and repair company which creates and acts upon our car objects.
What about trucks? Trucks could certainly use the same properties, methods and events as a car, but then maybe have some additional members, like say a property indicating if it has a cap or not on the truck bed. Trucks could have a class that inherits from the car class, and then adds its own members like the property “cap”.
Classes have levels of privacy as well – perhaps the amount you paid for the car or truck should be private as opposed to the color which could be public.
There are many more facets we could get into (and we will get into some more in our remaining episodes of this series) but hopefully this provides you with a little background on object-oriented concepts before we dig into our “Magic Menu” C# code!