Summary

  • Overloading is the act of creating two or more methods with the same name, but that differ in the number, type of parameters, or both.

  • Properties appear to clients to be members, but appear to the designer of the class to be methods. This allows the designer to modify how the property retrieves its value without breaking the semantics of the client program.

  • Properties include get and set accessors that are used to retrieve and modify a member field, respectively. The set accessor has an implicit parameter named value that represents the value to be assigned through the property.

  • Automatic properties provide a shorthand way of creating properties, if all you want to do is set or retrieve a value, as opposed to doing any other processing in the accessor.

  • When you “pass by reference,” the called method affects the object referred to in the calling method. When you pass by value, the changes in the called method are not reflected in the calling method. You can pass value types by reference by using either the ref or the out keyword.

  • The out parameter eliminates the requirement to initialize a variable before passing it to a method.

Now you have an idea of how methods work, and just how essential they are to your classes. Classes with no methods can still be very useful for holding data, but they won’t be able to do much on their own. You’ll be seeing methods extensively throughout the rest of this book, to the point where you probably won’t even think much about them anymore. You may have noticed that method calls make the flow of your program much more complicated than the straight line it was in the early chapters. That’s entirely appropriate, but one side-effect of complicating the flow is that it can become harder to find bugs. If your program has methods that call other methods that call still more methods, and it crashes, where should you start to look for the bug? Fortunately, Visual Studio can help you out; it has a whole set of tools designed to help you find and diagnose bugs. We’ll take a quick break from the C# theory to examine those tools in Chapter 9. We think you’ll thank us for it in later chapters.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset