In addition to properties, most objects have methods. Methods are actions the object can perform, in contrast to attributes that describe the object. To understand this distinction, think about the Pet object example. A Dog object has a certain set of actions that it can perform. These actions, called methods in C#, include barking and tail wagging. Figure 3.6 illustrates the Dog object and its methods.
Think of methods as functionswhich is exactly what they are. When you invoke a method, code is executed. You can pass data to the method, and methods may return values. However, a method is neither required to accept parameters (data passed by the calling code) nor required to return a value; many methods simply perform an action in code. Invoking (triggering) a method is similar to referencing the value of a property; you first reference the object's name, then a "dot," then the method name, followed by a set of parentheses, which can optionally contain any parameters that must be passed to the method.
Figure 3.6 Invoking a method causes the object to perform an action.
For example, to make the hypothetical Dog object Bruno bark using C# code, you would use this line of code:
Method calls in C# must always have parentheses. Sometimes they'll be empty, but at other times they'll contain data.
Invoking methods is simple; the real skill lies in knowing what methods an object supports and when to use a particular method.
Understanding Method Dynamism
Properties and methods go hand in hand, and at times a particular method may become unavailable because of one or more property values. For example, if you were to set the NumberofLegs on the Dog object Bruno equal to zero, the Walk and Fetch methods would obviously be inapplicable. If you were to set the NumberofLegs property back to four, you could then trigger the Walk or Fetch methods again. In C#, a method or property won't physically become unavailableyou can still call it, but doing so might cause an exception (error) or the call may be ignored.