Methods

  • Receives input from the parameters and output back using the return type.
  • Can have void return type where it doesn’t return any value
  • Method signature must be unique within the type

Modifiers

  1. Static : static
  2. Access modifiers: public, internal, private, protected
  3. Inheritance modifiers: new, virtual, abstract, override, sealed
  4. Partial: partial
  5. Unmanaged: unsage, extern
  6. Asynchronous: async

Expression-bodied methods (C# 6)

Expression-bodied method uses fat arrow to replace braces and return keyword and can also have void type.

int Foo (int x) { return x * 2; }
equals to
int Foo (int x) => x * 2;
void method
void Foo (int x) => Console.WriteLine (x);

Overloading methods

Methods with same name with different signatures and same return type

Pass-by-value versus Pass-by-reference

Whether a parameter is pass-by-value or pass-by-reference is also part of the signature.
Foo(int) can coexist Foo( ref int) or Foo(out int) but Foo(ref int) cannot coexist Foo(out int).

Leave a Reply

Your email address will not be published. Required fields are marked *