Lesson 2:Methods in details



Methods and their return types and arguments lists

method can be termed as a collection of statement(s) + expression(s) which  perform specific tasks when invoked.
method mainly consists of access modifier(s) such as public, private, protected, internal etc, name, modifier (such as  static, override), return type, argument (s), members in the form of variables, operators  and a body

Example

using System;
using System.Text;

namespace ConsoleApplication1
{
class Program
{

static void Main(string[ ] args)
{

int a = printValue(2); /* method call here also termed as invoking
which returns an integer value to the variable “a”.2 is the argument value supplied */

Console.WriteLine(a); // print the value inside ‘a’

}

static int printValue(int a) /* static modifier to call a function directly without instiating,int is the return value type
printValue is the name of the function*/

{

return a;
}

}
} // The { } brackets represent the method’s body

Output will be 2

Ref and Out

Ref and Out are often used in methods as their arguments. Sometimes, it is quite necessary to point out the variable not by its value but by its reference (location or address of the variable) in the memory. In simple words; a variable is pointed by its address in memory rather than by its value.
The need for this type of programming is to prevent the change in the actual value of the variable.

Examples

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main()
{
char chr = ‘a’;

DisplayVal1(chr);
Console.WriteLine(chr); /*The value shows ‘a’ because when main() is called
it has set chr=’a’ hence the value has changed */
DisplayVal2(ref chr);

Console.WriteLine(chr); /*The value shows ‘b’ because when main() is called.
Although it has set chr=’a’ but DisplayVal2 () refers the value stored at the address not
the current value of chr.Hence ‘b’ is printed */

DisplayVal3(out chr);
Console.WriteLine(chr); /*The value shows ‘c’ because when main() is called.
Although it has set chr=’a’ but DisplayVal3 () refers the value stored at the address not
the current value of chr.Hence ‘c’ is printed */

}

static void DisplayVal1(char character)
{
character =’b’ ;
}

static void DisplayVal2(ref char character)
{
character = ‘b’;
}

static void DisplayVal3(out char character)
{

character =’c’;
}

}
}

Ref Vs Out

Simply look at these

static void DisplayVal3(out char character)
{
character =’c’;
}

The above code is correct because the argument/parameter character  is assigned a particular value i.e ‘c’ in its body. If we make the changes in  the above example and don’t assign any value to the variable ‘character’,then error will be thrown.This is not the case with Ref as Ref  doesn’t care about the assigning the parameter(s) with the value(s)Out always make sure that the variable has to be assigned a certain value.Out is more reliable in strongly type C#  language than Ref

 

// Error

static void DisplayVal3(out char character)
{

}

 

//Compiled without error

static void DisplayVal3(ref char character)
{

}

 

 

Lesson 2 completed……..

 

Copyrighted.com Registered & Protected 
XWIJ-IKGX-KU21-IFCN

Syed Adeel Ahmed
Analyst, Programmer, Educationist and Blogger at Technofranchise

Computer Systems Engineer from Sir Syed University Of Engineering & Technology.I am passionate about all types of programming.


Syed Adeel Ahmed on FacebookSyed Adeel Ahmed on GoogleSyed Adeel Ahmed on InstagramSyed Adeel Ahmed on Twitter

Published by

Syed Adeel Ahmed

Computer Systems Engineer from Sir Syed University Of Engineering & Technology.I am passionate about all types of programming.