| Start |
|---|
| Calling a Method Calling a static method: Calling a static method and storing its return value: Calling an instance method: Calling a generic method |
| Anonymous method Anonymous methods provide a technique to pass a code block as a delegate parameter. They are methods with a body, but no name. |
| Declaring a Method Every method has a unique signature consisting of a accessor (
a Method may have some or no input parameters. to set parameters for a method, you should declare each one like normal variable declarations (like Parameters may have default values. for this you should set a value for the parameter (like The following method example returns the sum of two integers: |
| Parameters and Arguments A method can declare any number of parameters (in this example, Parameters can be used to pass values into a method, so that the method can work with them. This can be every kind of work like printing the values, or making modifications to the object referenced by a parameter, or storing the values. When you call the method, you need to pass an actual value for every parameter. At this point, the values that you actually pass to the method call are called Arguments: |
| Return Types A method can return either nothing ( If your method specifies a return value, the method must return a value. You do this using the If your method returns nothing ( Examples of valid Throwing an exception can end method execution without returning a value. Also, there are iterator blocks, where return values are generated by using the yield keyword, but those are special cases that will not be explained at this point. |
| Default Parameters You can use default parameters if you want to provide the option to leave out parameters: When you call such a method and omit a parameter for which a default value is provided, the compiler inserts that default value for you. Keep in mind that parameters with default values need to be written after parameters without default values. WARNING: Because it works that way, default values can be problematic in some cases. If you change the default value of a method parameter and don't recompile all callers of that method, those callers will still use the default value that was present when they were compiled, possibly causing inconsistencies. |
| Method overloading Definition : When multiple methods with the same name are declared with different parameters, it is referred to as method overloading. Method overloading typically represents functions that are identical in their purpose but are written to accept different data types as their parameters. Factors affecting
Consider a method named Example This method will accepts one argument and return a string, if we call the method with an integer(say Similarly if we pass two double values to this method the output will be the product of the two values and are of type double. This can be used of multiplication as well as finding the Area of rectangles This can be used specially for finding the area of circle, which will accepts a double value( Each of these methods can be called normally without conflict - the compiler will examine the parameters of each method call to determine which version of **Note that return type alone cannot differentiate between two methods. For instance, if we had two definitions for Area that had the same parameters, like so: If we need to have our class use the same method names that return different values, we can remove the issues of ambiguity by implementing an interface and explicitly defining its usage. |
Access rights |
Read Article
https://develop.horje.com/learn/1434/reference