C# Examples

C# Example Code


C# Methods

In C#, methods are a means of providing block of code that can be executed at any point in an application. The purpos of writing an method is to avoid writting repetated code for same output. You can create method that calculate the maximum value in an given array and use this method from any point in your code. This method can be thought of as containing reusable code.

Method have advantage of making your code cleane, readable and reusable, as the purpos of to keep related code together.

Defining and calling Methods

Usually, bellow syntax are used to define the method in CSharp

Syntax

<access specifier> <return type> (parameter list)
{
   method Body comes here......
}

You can find various elements of method

Access specifire - Access specifire is used to determine the accessibility of method and can be public, private and protected.

Return Type - Method may return a value of specific type. Type can be any CSharp built in data type or custom data type. If method does not returning any values, then the return type is void.

Method Name - Method name is a unique identifier and it is case sensitive.

Parameters - Method may or may not be accept parameters. Parameter can be value type or reference type, the parameters are used to pass and receive data from a method.

Method Body - Method body contains group of related code which is responsible to perform an specific operation.