Methods

A method is a procedure or function that is declared inside a class.

public class MyClass 
{
   public int MyMethod1() {}
   private void MyMethod2() {}
}

When a method doesn't need to return a value it is declared as returning void
You must always include parentheses when calling methods regardless of whether a value is returned.


Non-Virtual Method

The method cannot be redefined in a derived class


Return Statement

The return statement terminates execution of the method and returns control to the calling method.
If the method is a void then the return statement can be omitted.
If the return statement is in a try-block-catch then the finally will still be executed



Exiting Methods

Assigning Function Name
Assigning the function name equal to the return value does not automatically exit the function.


Return
This can be used inside a Function
This doesn't exit the subroutine it just defines the value to return.


Does Return exit the sub and function - YES !!!
VB.Net - Return True exits the method
You do not need to exit sub or exit function on the subsequent line


void DoSomething(int[] ArrayInt) 
{
   ArrayInt[1] = 10;
}

void DoSomething(string[ , ] MultiArrayString) 
{
   MultiArray[1,1] = "one";
}

The array must be declared outside
The array must be initialised inside.

void MyMethod(out int[] ArrayInt) 
{
   ArrayInt = new int[10];
}

The array must be declared outside
The array can be initialised either inside or outside.

void MyMethod(ref string[] ArrayString) 
{
   ArrayString[1] = "one";
}

Overload Resolution

These are methods with the same name but the parameter types are different
Before the introduction of optional and named parameters the method overloading resolution was based on parameter types



© 2019 Better Solutions Limited. All Rights Reserved. © 2019 Better Solutions Limited TopPrevNext