Methods
Eng Teong Cheah
Microsoft MVP Windows Development
Xamarin: the complete mobile lifecycle solution
Methods
Methods
Methods are used to define logic once and use it at many places, which make
maintenance of program easier.
Syntax of declaring methods”
[Attributes]
access-modifiers return-type methodname( parameters )
{
methodbody
}
Methods
Access modifiers can be private, public, protected, internal, protected internal.
- Return type can be any valid date type or void if nothing is returned by method.
- Method name is name of the method.
- Parameters can be any data type. Parameters are optional.
Methods are of two types:
- Static Methods
- Instance methods
Static methods
Static Methods are associated with class. Example:
class Program
{
//declaring and using static method
public static void print()
{
Console.WriteLine("printing done");
}
static void Main(string[] args)
{
print();
Console.ReadKey();
}
}
Instance methods
Instance methods are associated with instance. Example:
class Program
{
//declaring and using instance method
public void print()
{
Console.WriteLine("printing done");
}
static void Main(string[] args)
{
Program p = new Program();
p.print();
Console.ReadKey();
}
}
Methods parameters
There are four types of method parameters:
- Value parameters
Passing parameter by value
- Reference parameters
Passing parameter by reference
- Out parameters
Out parameter is used when we want multiple values output from single method and
don’t know the initialization value of input parameter.
- Parameters arrays
Passing array as parameter
Value parameters
Passing parameter by value:
class Program
{
//passing parameter by value
public void increment(int i)
{
++i;
}
static void Main(string[] args)
{
Program p = new Program();
int j = 10;
p.increment(j);
Console.WriteLine(j);
Console.ReadKey();
}
}
Result :-
10
Reference parameters
Keyword ref is use for passing the value type by reference.
Passing parameter by reference:
class Program
{
//passing parameter by reference
public void increment(ref int i)
{
++i;
}
static void Main(string[] args)
{
Program p = new Program();
int j = 10;
p.increment(ref j);
Console.WriteLine(j);
Console.ReadKey();
}
}
Out parameters
Out parameters is used when we want multiple values output from single method and
don’t know the initialization value of input parameter.
class Program
{
//passing out parameter
public int initialize(out int i)
{
i = 10;
int j = i + 1;
return j;
}
static void Main(string[] args)
{
Program p = new Program();
int j;
int increasedvalue = p.initialize(out j);
Console.WriteLine("value and incremented value is {0} {1}", j, increasedvalue);
Console.ReadKey();
}
}
Parameter arrays
Passing array as parameter:class Program
{
//passing array as parameter
public int[] initialize(int[] arr)
{
for (int i = 0; i < arr.Length - 1; i++)
{
arr[i] = i;
}
return arr;
}
static void Main(string[] args)
{
Program p = new Program();
int[] arr = new int[10];
// passing array as parameter;
p.initialize(arr);
for (int i = 0; i < arr.Length - 1; i++)
{
Console.WriteLine(arr[i]);
}
Console.ReadKey();
}
}
Optional and Named parameters
Using Optional parameters:
class Program
{
// length and breadth are optional parameter
public void print(int pages, int length = 100, int breadth = 40)
{
Console.WriteLine("length, breadth, no of pages {0} {1} {2}", length, breadth, pages);
}
static void Main(string[] args)
{
Program p = new Program();
p.print(10);
Console.ReadKey();
}
}
Optional and Named parameters
Using Named parameters:
class Program
{
public void print(int pages, int length, int breadth)
{
Console.WriteLine("length, breadth, no of pages {0} {1} {2}", length, breadth, pages);
}
static void Main(string[] args)
{
Program p = new Program();
// passing parameters by name
p.print(length: 10, breadth: 16, pages: 10);
Console.ReadKey();
}
}
Demo
Get Started Today
Xamarin.com
•Website:
• http://techoschool.com/
•Get Started:
• http://xamarin.com
Reference

Xamarin: C# Methods

  • 1.
    Methods Eng Teong Cheah MicrosoftMVP Windows Development
  • 2.
    Xamarin: the completemobile lifecycle solution
  • 3.
  • 4.
    Methods Methods are usedto define logic once and use it at many places, which make maintenance of program easier. Syntax of declaring methods” [Attributes] access-modifiers return-type methodname( parameters ) { methodbody }
  • 5.
    Methods Access modifiers canbe private, public, protected, internal, protected internal. - Return type can be any valid date type or void if nothing is returned by method. - Method name is name of the method. - Parameters can be any data type. Parameters are optional. Methods are of two types: - Static Methods - Instance methods
  • 6.
    Static methods Static Methodsare associated with class. Example: class Program { //declaring and using static method public static void print() { Console.WriteLine("printing done"); } static void Main(string[] args) { print(); Console.ReadKey(); } }
  • 7.
    Instance methods Instance methodsare associated with instance. Example: class Program { //declaring and using instance method public void print() { Console.WriteLine("printing done"); } static void Main(string[] args) { Program p = new Program(); p.print(); Console.ReadKey(); } }
  • 8.
    Methods parameters There arefour types of method parameters: - Value parameters Passing parameter by value - Reference parameters Passing parameter by reference - Out parameters Out parameter is used when we want multiple values output from single method and don’t know the initialization value of input parameter. - Parameters arrays Passing array as parameter
  • 9.
    Value parameters Passing parameterby value: class Program { //passing parameter by value public void increment(int i) { ++i; } static void Main(string[] args) { Program p = new Program(); int j = 10; p.increment(j); Console.WriteLine(j); Console.ReadKey(); } } Result :- 10
  • 10.
    Reference parameters Keyword refis use for passing the value type by reference. Passing parameter by reference: class Program { //passing parameter by reference public void increment(ref int i) { ++i; } static void Main(string[] args) { Program p = new Program(); int j = 10; p.increment(ref j); Console.WriteLine(j); Console.ReadKey(); } }
  • 11.
    Out parameters Out parametersis used when we want multiple values output from single method and don’t know the initialization value of input parameter. class Program { //passing out parameter public int initialize(out int i) { i = 10; int j = i + 1; return j; } static void Main(string[] args) { Program p = new Program(); int j; int increasedvalue = p.initialize(out j); Console.WriteLine("value and incremented value is {0} {1}", j, increasedvalue); Console.ReadKey(); } }
  • 12.
    Parameter arrays Passing arrayas parameter:class Program { //passing array as parameter public int[] initialize(int[] arr) { for (int i = 0; i < arr.Length - 1; i++) { arr[i] = i; } return arr; } static void Main(string[] args) { Program p = new Program(); int[] arr = new int[10]; // passing array as parameter; p.initialize(arr); for (int i = 0; i < arr.Length - 1; i++) { Console.WriteLine(arr[i]); } Console.ReadKey(); } }
  • 13.
    Optional and Namedparameters Using Optional parameters: class Program { // length and breadth are optional parameter public void print(int pages, int length = 100, int breadth = 40) { Console.WriteLine("length, breadth, no of pages {0} {1} {2}", length, breadth, pages); } static void Main(string[] args) { Program p = new Program(); p.print(10); Console.ReadKey(); } }
  • 14.
    Optional and Namedparameters Using Named parameters: class Program { public void print(int pages, int length, int breadth) { Console.WriteLine("length, breadth, no of pages {0} {1} {2}", length, breadth, pages); } static void Main(string[] args) { Program p = new Program(); // passing parameters by name p.print(length: 10, breadth: 16, pages: 10); Console.ReadKey(); } }
  • 15.
  • 16.
  • 17.