Passing Arguments and Returning Values to / from Methods

As you know in C++, the member methods can receive one or more arguments and also can return a value at the end of the method.
Passing Arguments and Returning Values

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


namespace ArgsAndReturnDemo
{
class MyNumbers
{
//data members
private int a, b;
//methods
public void SetValues(int FirstValue, int SecondValue)
{
a = FirstValue;
b = SecondValue;
}
public void ShowValues()
{
Console.WriteLine("The values are " + a + " and " + b);
}
public int GetMax()
{
if (a > b)
return (a);
else
return (b);
}
public int GetMin()
{
if (a < b)
return (a);
else
return (b);
}
}
class Program
{
static void Main(string[] args)
{
//read values from keyboard
int FirstVal, SecondVal;
Console.WriteLine("Enter any two values:");
FirstVal = Convert.ToInt32(Console.ReadLine());
SecondVal = Convert.ToInt32(Console.ReadLine());
//create instance of MyNumbers class
MyNumbers mn;
mn = new MyNumbers();
//access methods
mn.SetValues(FirstVal, SecondVal);
mn.ShowValues();
Console.WriteLine("The maximum value is " + mn.GetMax());
Console.WriteLine("The minimum value is " + mn.GetMin());
Console.Read();
}
}
}

Method Overloading

“Ref” Parameters

• The reference parameters are similar to the normal parameters.
• The only difference between the normal parameters and reference parameters is: When the value is changed in the reference parameter, would automatically affect the actual parameter in the calling portion.
• Rule: The actual parameter at the calling portion, can’t be a constant.
• This is just like “Call by reference” concept in C/C++.

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


namespace RefParameterDemo
{
class SampleClass
{
public void FirstMethod(int x, int y)
{
x++;
y++;
}
public void SecondMethod(int x, ref int y)
{
x++;
y++;
}
}
class Program
{
static void Main(string[] args)
{
int a = 10, b = 20;
SampleClass sc = new SampleClass();
Console.WriteLine(a + ", " + b);
sc.FirstMethod(a, b);
Console.WriteLine(a + ", " + b);
sc.SecondMethod(a, ref b);
Console.WriteLine(a + ", " + b);
Console.Read();
}
}
}
Method Overloading

Method Overloading

• Just like in C++, C# allows you to overload the methods.
• Method overloading is nothing but writing multiple methods with same name.
• To overload methods, you should follow the below rules.
1. All of the overloaded methods name should be same.
2. Any difference between the method arguments should be maintained. The difference may be in no. of arguments or the data types of arguments.

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

namespace SimpleMethodOverloading
{
class SimpleOverloadDemo
{
public void Show(int n)
{
Console.WriteLine("An integer value is found: " + n);
}
public void Show(double d)
{
Console.WriteLine("A double value is found: " + d);
}
public void Show(string s)
{
Console.WriteLine("A string value is found: " + s);
}
}
class Program
{
static void Main(string[] args)
{
SimpleOverloadDemo sod = new SimpleOverloadDemo();
sod.Show(92);
sod.Show(1043.3948);
sod.Show("Hello, World!");
Console.Read();
}
}
}

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

namespace MethodOverloading
{
class OverloadSample
{
public int GetMax(int a, int b)
{
if (a > b)
return (a);
else
return (b);
}
public double GetMax(double x, double y)
{
if (x > y)
return (x);
else
return (y);
}
public double GetMax(double x, double y, double z)
{
if (x > y && x > z)
return (x);
else if (y > x && y > z)
return (y);
else
return (z);
}
}
class Program
{
static void Main(string[] args)
{
OverloadSample os = new OverloadSample();
Console.WriteLine(os.GetMax(15, 20));
Console.WriteLine(os.GetMax(87.12, 102.8273, 98.56 ));
Console.WriteLine(os.GetMax(87.56, 45.12 ));
Console.Read();
}
}
}

Method Overloading
Recursion
• Same as C/C++.
Operator Overloading
• Same as C++, but the “operator” method should be static.