Anonymous methods provide a technique to pass a code block as a delegate parameter. They are methods with a body, but no name.
Calling a Method
Calling a static method:
Calling a static method and storing its return value:
Calling an instance method:
Calling a generic method
Declaring a Method
Every method has a unique signature consisting of a accessor (
private, ...) ,optional modifier (
abstract), a name and if needed method parameters.
Note, that the return type is not part of the signature. A method prototype looks like the following:
AccessModifier can be
pirvate or by default
OptionalModifier can be
ReturnType can be
void for no return or can be any type from the basic ones, as
int to complex classes.
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
int a), and for more than one parameter you should use comma between them (like
int a, int b).
Parameters may have default values. for this you should set a value for the parameter (like
int a = 0). if a parameter has a default value, setting the input value is optional.
The following method example returns the sum of two integers:
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.
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.
- Number of Arguments
- Type of arguments
- Return Type**
Consider a method named
Area that will perform calculation functions, which will accepts various arguments and return the result.
This method will accepts one argument and return a string, if we call the method with an integer(say
5) the output will be
"Area of Square is 25".
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(
radius) and return another double value as its Area.
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
Area needs to be used.
**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.
Parameters and Arguments
A method can declare any number of parameters (in this example,
o are the parameters):
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:
A method can return either nothing (
void), or a value of a specified type:
If your method specifies a return value, the method must return a value. You do this using the
return statement. Once a
return statement has been reached, it returns the specified value and any code after it will not be run anymore (exceptions are
finally blocks, which will still be executed before the method returns).
If your method returns nothing (
void), you can still use the
return statement without a value if you want to return from the method immediately. At the end of such a method, a
return statement would be unnecessary though.
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.