Declaring a Method

Download c# eBook

Example

Every method has a unique signature consisting of a accessor (public, 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 OptionalModifier ReturnType MethodName(InputParameters)
{
    //Method body
}

AccessModifier can be public, protected, pirvate or by default internal.

OptionalModifier can be static abstract virtual override new or sealed.

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:

private int Sum(int a, int b)
{
    return a + b;
} 

Stats

Contributors: 5
2016-08-05
Licensed under: CC-BY-SA

Not affiliated with Stack Overflow
Rip Tutorial: info@zzzprojects.com

Download eBook