Posts

Showing posts from November, 2017

FAQ

What is the purpose of a method ? To implement the logic required for specific behaviour or functionality in a class. What is the difference between a parameter and an argument? A parameter is part of the method signature An argument is part of the method call What is method overloading ? Methods with the same name and purpose, but different signatures. What us method chaining ? One method overload called another overload to prevent repeated code. When is it best to use method overloading vs method overriding? Use overloading when one method requires multiple signatures Use overriding when replacing a method defined higher up in the object hierarchy.

Method Overriding

Image
Method overriding allows us to completely replace the operation of a method that is defined further up in the object hierarchy as indicated by inheritance relationships. To make sense of overriding, let's look at basic inheritance in the .net framework. Everything in .net inherits from the .net framework system class called object. So int, the .net integer, inherits from object, string inherits from object, and classes we create inherit from object. The base class has built-in properties and methods, such as a ToString method. The base ToString method returns the fully qualified string name of the class. Any child class can use properties and methods of any class above it in the hierarchy. Because Product and Vendor inherit from Object, they both provide the ToString method. What if we want ToString method to do something else when it is called on a Product object? For example, say we want to display detailed information about that product, such as the product ...

Method Chaining

Method chaining is a technique whereby one method overload calls another method overload. The purpose of method chaining is to reduce repeated code between the overloads, so there is one primary overload with the majority of the code. Do consider using method chaining to minimize repeated code in method overloads. Do consider using an optional parameter instead of overloads and chaining to minimize repeated code. Avoid if it adds complexity.  

Method Overloading

Image
There are a lot of techniques for working with methods. One technique is called method overloading .  Method overloading allows us to define methods with the same name and purpose, but different signatures. The first PlaceOrder method defines two parameters, one for the product to order and one for the quantity of that product. This overload adds a delivery by date, and another overload adds delivery instructions. This overload adds a delivery by date, and another overload adds delivery instructions. For method overloading, the name must be the same, and the number and/or type of parameters must be different. The return type is excluded when comparing signature for overloading, so even though this method has the name, it is not a valid overload, because the only difference is the return type.  Overloading lets us provide a simple signature for the simple scenarios and more full-featured signatures for more advanced scenarios, making it easier for other cod...

Property or Method

As we have seen, properties are for data and methods are for operations. Here are some guidelines to help us with decision property or methods. Property Does it describe data? for example, product name and description. Does it execute quickly,  Method Does it describe processing? for example, place an order.  Does it produce side effects? Does it require a parameters? 

Building a Method

Image
The code we write for a method includes a   method signature defining the method name, parameters, and return type the body implementing the code for the method method return statement. Here is an example of a method signature. A method signature optionally begins with an accessibility modifier. the default is private. After the accessibility modifier is the return type. this is the type of value returned by the method. void if no return type. Next is the method name, methods names are PascalCase.method name are often verbs or verbs with objects, since the purpose of the method is to perform an action. Last is the parameter list, defining the data that must be passed to this method. this gives the method the data that it needs to perform its operation. For each parameter, the data type and a variable name are defined. the parameters are enclosed in parentheses and separated by commas. It is always best practice to add XML document comments for t...
Image
A method is code block that contains a set of programming statements. Also called a function Purpose: To implement the logic required for specific behaviour or functionality in a class. Identify the problem that method should solve. will this method calculate and return a value?  Ensure the method has a single purpose. clarifying the purpose will help determine which class the method belongs to. Specify the inputs and the outputs, what data the method need? what data should the method return?  State any assumptions. Does the incoming data need to be within a range of value or date after the current date? Whatever the assumptions are, state them.  Consider the errors that could occur.