Tutorial by Examples

Every method has a unique signature consisting of a accessor (public, private, ...) ,optional modifier (abstract), a name and if needed method parameters. Note, that the return type is not part of the signature. A method prototype looks like the following: AccessModifier OptionalModifier ReturnTyp...
Calling a static method: // Single argument System.Console.WriteLine("Hello World"); // Multiple arguments string name = "User"; System.Console.WriteLine("Hello, {0}!", name); Calling a static method and storing its return value: string input = System.Con...
A method can declare any number of parameters (in this example, i, s and o are the parameters): static void DoSomething(int i, string s, object o) { Console.WriteLine(String.Format("i={0}, s={1}, o={2}", i, s, o)); } Parameters can be used to pass values into a method, so that th...
A method can return either nothing (void), or a value of a specified type: // If you don't want to return a value, use void as return type. static void ReturnsNothing() { Console.WriteLine("Returns nothing"); } // If you want to return a value, you need to specify its type. st...
You can use default parameters if you want to provide the option to leave out parameters: static void SaySomething(string what = "ehh") { Console.WriteLine(what); } static void Main() { // prints "hello" SaySomething("hello"); // prints &quot...
Definition : When multiple methods with the same name are declared with different parameters, it is referred to as method overloading. Method overloading typically represents functions that are identical in their purpose but are written to accept different data types as their parameters. Factors af...
Anonymous methods provide a technique to pass a code block as a delegate parameter. They are methods with a body, but no name. delegate int IntOp(int lhs, int rhs); class Program { static void Main(string[] args) { // C# 2.0 definition IntOp add = delegate(int lhs,...
// static: is callable on a class even when no instance of the class has been created public static void MyMethod() // virtual: can be called or overridden in an inherited class public virtual void MyMethod() // internal: access is limited within the current assembly internal void MyMetho...

Page 1 of 1