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




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



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