Syntax to write a method
public void print(int x, int y)
{
///Logic
}
Observation:
if returntype is otherthan void, Then in the logic return statement must be provided. Return is a keyword
->C#.NET supports to pass the parameters in 3 ways
1)call by value
2)call by reference
3)call by out
Example: on callbyvalue and callby reference
Open windows forms application project:
Start->programs->Microsoft visual studio 2010->Microsoft Visual studio 2010->file menu->new->
project->select visual c# from installed templates->select windows forms application project
open toolbox [view->toolbox]
place a button (double click on button)
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
class Test
{
public void swap(int x, ref int y)
{
int t = x; int X = y;
y = t;
}
}
private void button1_Click(object sender, EventArgs e)
{
Test t = new Test();
int a = 10, b = 20;
t.swap(a, ref b);
MessageBox.Show(a+" "+b);
}
}
}
Go to F5(output)
Working with call by out
1)out is keyword
2)out&ref keywords are 99% same
3)out keyword must be used along with actual&formal args
4)the variables, which are passing by out is not must to initialize
5)even if out variable is initialized,The value will not be considered
6)out=ref-initilization
Example: on call by out
Open windows forms application project:
Start->programs->Microsoft visual studio 2010->Microsoft Visual studio 2010->file menu->new->
project->select visual c# from installed templates->select windows forms application project
open toolbox [view->toolbox]
place a button (double click on button)
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
class Emp
{
public void calculate(int s, int i, out int ts)
{
ts = s + i;
}
}
private void button1_Click(object sender, EventArgs e)
{
Emp e1 = new Emp();
int sal = 5000, inc = 3000, total;
e1.calculate(sal, inc, out total);
MessageBox.Show(total + " ");
}
}
}
Go to F5(output)
Working with polymorphism
Polymorphism also called as overloading.
C#.Net supports 2 types of overloading concepts
1)method overloading
2)operator overloading
Working with method overloading:
Method overloading is a concept of writing more than one method with same name and with different types of args
Example on method overloading
Open windows forms application project:
Start->programs->Microsoft visual studio 2010->Microsoft Visual studio 2010->file menu->new->
project->select visual c# from installed templates->select windows forms application project
open toolbox [view->toolbox]
place a button (double click on button)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication4
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
class Test
{
public void Display(float x)
{
MessageBox.Show("display with float is called");
}
public void Display(double x)
{
MessageBox.Show("display with double is called");
}
public void Display(decimal x)
{
MessageBox.Show("display with decimal is called");
}
} // close Test class
private void button1_Click(object sender, EventArgs e)
{
Test t = new Test();
t.Display(4.0);
t.Display(4f);
t.Display(4d);
t.Display(4m);
}
}
}
Go to F5(output)