|
Methods help you to separate code into modules that perform a given task. Every program or application have a starting entry point to there application that decide the path execution and all. We already come across different application where the starting point or entry point is main ( ) method. Methods provide the flexibility where we can separate the code into modules. This will make easy understanding of code, more reusability and increase performance. The method can pass parameters to another method and return the value that is optional. If the method contains “void” keyword, this means method will not return any thing.
Syntax:
access specifier return-type methodname (parameter list) {
//Statement
}
Method Chaining:- The method chaining is the process by which one method calls another method. The caller method can pass parameters to the called method and in return the called method return some value (this is optional).
Example
The following example will demonstrate the method concepts along with the method chaining.
using System;
using System.Collections.Generic;
using System.Windows.Forms;
namespace cSHARPEXAMPLES
{
static class Program
{
[ STAThread ]
static void Main ()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault( false );
Application.Run( new Form1 ());
}
}
}

Method Chaining: Here we can add two values to the form and User can select the options i.e. 1 Add, 2 Subtract, 3 Multiply and 4 Divide. And result will be displayed when the user click Execute.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace cSHARPEXAMPLES
{
public partial class Form4 : Form
{
public Form4()
{
InitializeComponent();
}
private void Form4_Load( object sender, EventArgs e)
{
lblEnter.Text = "" ;
lblEnter.Text = lblEnter.Text + "Enter 1 to Add" + "\n" ;
lblEnter.Text = lblEnter.Text + "Enter 2 to Sub" + "\n" ;
lblEnter.Text = lblEnter.Text + "Enter 3 to Multiply" + "\n" ;
lblEnter.Text = lblEnter.Text + "Enter 4 to Divide" + "\n" ;
}
private void btnExecute_Click( object sender, EventArgs e)
{
int _valDec = Int32 .Parse(txtdecision.Text);
int _firstVal = Int32 .Parse(textBox1.Text);
int _secondVal = Int32 .Parse(textBox2.Text);
switch (_valDec)
{
case 1: fxadd(_firstVal, _secondVal); break ;
case 2: fxsubtract(_firstVal, _secondVal); break ;
case 3: fxmultiply(_firstVal, _secondVal); break ;
case 4: fxdivide(_firstVal, _secondVal); break ;
default :
MessageBox .Show( "Please Enter Specified Value.." );
break ;
}
}
void fxadd( int firstarg, int secondarg) {
int _add = firstarg + secondarg;
txtResult.Text = _add.ToString();
}
void fxsubtract( int firstarg, int secondarg) {
int _sub = (firstarg - secondarg);
txtResult.Text = _sub.ToString();
}
void fxmultiply( int firstarg, int secondarg){
int _mul = firstarg * secondarg;
txtResult.Text = _mul.ToString();
}
void fxdivide( int firstarg, int secondarg){
int _div = firstarg / secondarg;
txtResult.Text = _div.ToString();
}
}
}
Output

|