Author : MD TAREQ HASSAN | Updated : 2021/03/22
Method
accessModifier returnType MethodName(type parameter){
// .Net convention => method name starts with Capital letter
// method signature => method name + parameters (return type does not count)
}
Default parameter
// Default parameter / Optional Argument
public void GetFoo(int bar, var baz = 10){
// Optional Arguments must be at the end
}
Named argument
// Named argument
public void GetFoo(int bar, var baz = 10){
}
GetFoo(baz: 5, bar: 2) // argument order can be different is called with name
GetFoo(2, baz: 5) // If you mix arguments then named arguments must be at the end
Variable parameter
// Variable parameter
public static void UseParams(bool flag, params int[] list) {
/*
# The params parameter must be a single dimensional array
# params must be at the end of parameter
# only one params keyword is permitted in a method
*/
}
UseParams(flag: true, 1, 2, 3, 4);
// ref
public int ExampleMethod(ref int x){ // x must be initialized
}
int val = 10;
ExampleMethod(ref val);
// out
public int ExampleMethod(out int x){ // x does not have to be initialized
// out parameter must be initialized here before return
}
ExampleMethod(out val);
// Ref return
public ref double GetEstimatedDistance()
{
return ref estDistance;
}
ref int distance = GetEstimatedDistance() // only ref local variable can catch value returned by ref return method
Ref and Out for method signature
ref and out are not part of method signature. Therefore, methods cannot be overloaded if the only difference is that one method takes a ref argument and the other takes an out argument. following is error
public void SampleMethod(out int i) { }
public void SampleMethod(ref int i) { }
Expression body definitions
// Expression body definitions
public int GetFoo(int bar) => bar + 10;
public static Complex operator +(Complex a, Complex b) => a.Add(b);
public Customer this[long id] => store.LookupCustomer(id);
// calling base class methos from derived class
public Class Hassan : Person {
public new int GetAge(){
return base.GiveAge(); // Calling method of base class that has been hidden
}
}
Anonymous method
public delegate int MyDelegate(int k); // declared at namespace level
// ... ... ...
MyDelegate Foo = delegate (int k) {
return k * k;
};
var y = Foo(12) // 144
Overriding
override : use virtual keyword in base class and override keyword in sub class
Shadowing
override and use new keyword in sub-class (hides parent class implementation)
public class Foo {
public virtual void DoIt() {}
}
public class Bar: Foo {
public override new void DoIt() {}
}
Local Function
- C# 7.0+
- They can only be called from their containing member
- Local functions can be declared in and called from:
- Methods, especially iterator methods and async methods
- Constructors
- Property accessors
- Event accessors
- Anonymous methods
- Lambda expressions
- Finalizers
- Other local functions
- local functions can’t be declared inside an expression-bodied members
- Local functions can use the async and unsafe modifiers.
- all local variables that are defined in the containing member, including its method parameters, are accessible in the local function.
- cannot include the member access modifier. Because all local functions are private, including an access modifier, such as the private keyword
- Local functions allow your code to fail fast and allow your exception to be both thrown and observed synchronously
- Local functions (C# Programming Guide)
private static string GetFoo(int x) {
var foo = GetBar() + 10
return foo;
// Declare a local function.
string GetBar(){
//... ... ...
return x / 2;
}
}
Generic Method
public int Foo<T>(T bar){
// scope of T is only within the method
}