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.
Helper Methods
This is used to perform a common task across multiple classes
Do we use Static
Default Methods
Default Interface Methods ?
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
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.
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
Extension Methods
This is a special type of static method that has a slightly different syntax.
Extension methods allow you to add methods to existing types without having to derive a new type.
Extension methods are a special kind of static method but they are called as if they were instance methods on the extended type.
You can use extension methods to extend a class or interface but not to override it.
Extension methods are only in scope when you explicitly import the namespace.
Allows you to "add" methods to existing types without creating a new derived class or modifying the original type
msdn.microsoft.com/en-GB/library/bb383977.aspx
What is your extension method has the same name as an existing method name?
An extension method with the same name and signature will never be called as an instance member
It can only be called as a static method
Extension methods are defined as static members
The first parameter specifies which type the method operates on.
This parameter is preceeded by the 'this' modifier.
Extension methods cannot access private variables in the type.
Extension methods must be defined in a static class
You invoke the extension method using instance method syntax
Extending System.String
For simple types like System.String the extension methods are not shown in the intellisense
namespace ExtensionMethods
{
public static class MyExtensions
{
public static int WordCount(this System.String str)
{
return str.Split(new char[] {' ','.', '?'}, String.SplitOptions.RemoveEmptyEntries).Length
}
}
}
LINQ Standard Query Operators
One of the best examples of extension methods is the LINQ standard query operators.
Constructor
Extern Method
An extern modifier can be used to indicate that the implementation happens outside of the C# code.
This modifier is usually used with the DllImport attribute
[DllImport("User32.dll")]
public static extern GetDirectoryName(out string sDirName);
You can also use this modifier as an external assembly alias if you have two or more versions of the same assembly in the same application.
New
A new modifier can be used to replace a method in a base class.
The method defined in the derived class is completely independent of the method with the same name in the base class.
public class Base
{
public void F() {}
}
public class Derived : Base
{
public new void Method() {}
}
This is required when the method in the base class has not been declared as virtual.
Hidden members can still be called if an instance of the derived class is cast to an instance of the base class ??
Derived B = new Derived();
B.Method(); //calls the new method
Base B = new Derived();
A.Method(); //calls the old method
Cannot be used with override
Overload
You can have two or more methods in a class with the same name but different parameters, types and order of parameters.
void Method1() { .. }
void Method1(int a) { .. }
int Method1(int a) { .. }
int Method1(int a, int b) { .. }
Override
An override method indicates that this class provides a new implementation of a method inherited from the base class.
This overridden method must have exactly the same signature as the base method.
Applies to methods, properties, events, indexers
An override method indicates that it is providing a new implementation for a member in the derived class
The base class member must be virtual, abstract or override
The method must have exactly the same signature.
public class Base
{
public virtual void Method() {}
}
public class Derived : Base
{
public override void Method() {}
}
Cannot be used with 'static'
Generic Methods
Anonymous Methods
© 2026 Better Solutions Limited. All Rights Reserved. © 2026 Better Solutions Limited TopPrevNext