[attributes]
access-modifiers return-type method-name(parameters)
{
Method Body
}
--------------------------------------------
using System;
class Program
{
static void Main(string[] args) //static method
{
////1. In order to invoke EvenNumbers method, creating an instant of class (Program) is neccessory
//Program p = new Program(); // object of class
//p.EvenNumbers();
//2. invoke static method
Program.EvenNumbers();
}
//1. public void EvenNumbers() //instant method
public static void EvenNumbers() //2. static method
{
int Start = 0;
while (Start <= 20)
{
Console.WriteLine(Start);
Start += 2;
}
}
}
-------------------------------------------------
using System;
class Program
{
/* a method declaratin
* a static modifier - being a static method
* no static modifier - being an instance method
*
* invoking a method
* static - using the class name
* instance - using an instance of the class
*
* multiple instances of a class can be created (or instantiated), each instance has its own separate method
* but, there are no instances of that method, you can invoke only that one definition of the static method
*/
static void Main(string[] args) //static method
{
////1. In order to invoke EvenNumbers method, creating an instance of class (Program) is neccessory
//Program p = new Program(); // object of class
//p.EvenNumbers();
//2. invoke static method
Program.EvenNumbers();
//creating an instance of class, invoke Add method
Program p = new Program();
Console.WriteLine("Sum = {0}", p.Add(10, 30));
}
public int Add(int FN, int SN)
{
return FN + SN;
}
//1. public void EvenNumbers() //instance method
public static void EvenNumbers() //2. static method
{
int Start = 0;
while (Start <= 20)
{
Console.WriteLine(Start);
Start += 2;
}
}
}
---------------------------------------------
Method parameter types
- value parameters : creates a copy of the parameter passed, so modifications does not affect each other
- reference parameters : the ref method parameter keyword on a method parameter causes a method to refer to the same variable that was passed into the method. Any changes made to the parameter in the method with be reflected in that variable when control passed back to the calling method
- out parameters : Use when you want a method to return more than one value
- parameter arrays : The params keyword lets you specify a method parameter that takes a variable number of arguments. You can send a comma-separated list of arguments, or an array, or no arguments. params keyword should be the last one in a method declaration, and only one params keyword is permitted in a method declaration.
using System;
class Program
{
//pass by value
/*public static void Main()
{
int i = 0;
SimpleMethod(i); //Program.SimpleMethod(30); //as of a static method
Console.WriteLine(i);
}
public static void SimpleMethod(int j)
{
j = 101;
}
*/
//pass by referemce
/*public static void Main()
{
int i = 0;
SimpleMethod(ref i); //Program.SimpleMethod(30); //as of a static method
Console.WriteLine(i);
}
public static void SimpleMethod(ref int j)
{
j = 101;
}*/
//out parameters
/*public static void Main()
{
int Total = 0;
int Product = 0;
Calculate(10, 20, out Total, out Product);
Console.WriteLine("Sum = {0} && Product {1}", Total, Product);
}
public static void Calculate(int FN, int SN, out int Sum, out int Product)
{
Sum = FN + SN;
Product = FN * SN;
}*/
//parameter arrays
public static void Main()
{
int[] Numbers = new int[3];
Numbers[0] = 101;
Numbers[1] = 102;
Numbers[2] = 103;
//ParamsMethod(); //is possible as of params keyword
ParamsMethod(Numbers); //if not use params keyword, problem occurs
//ParamsMethod(1, 2, 3, 4, 5); //no limited put parameters
}
public static void ParamsMethod(params int[] Numbers) //params array should be the last one
{
Console.WriteLine("There are {0} elemnts", Numbers.Length);
foreach (int i in Numbers)
{
Console.WriteLine(i);
}
}
}
In method, called as parameters
during invoking a method, called arguments