Wednesday 30 May 2012

Out and Ref Keyword in C#.Net

Different Of out and ref Keyword in .net........................
  1.  both use for multiple return in single method means by using this we can  return multiple value or multiple output
  2. the  main  difference  between out and ref  are  that for ref we have  to intilize and out no need for intilize

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 out_ref
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

public void sum(int i, int j, out int ad, out int su, out int mu, out int di)
{
ad = i + j;
su = i - j;
mu = i * j;
di = i / j;
}

public void sumbyref(int i, int j, ref int ad, ref int su, ref int mu, ref int di)
{
ad = i + j;
su = i - j;
mu = i * j;
di = i / j;

}

private void button1_Click(object sender, EventArgs e)
{
int i = Convert.ToInt32(textBox1.Text);
int j = Convert.ToInt32(textBox2.Text);
int a, b, c, d;
sum(i, j, out a, out b, out c, out d);
MessageBox.Show("sum=" + a.ToString() + "subtraction =" + b.ToString() + "  " +"multiplication=" + c.ToString() + " " + "division =" + d.ToString());
}

private
 void button2_Click(object sender, EventArgs e)
{
int i = Convert.ToInt32(textBox1.Text);
int j = Convert.ToInt32(textBox2.Text);
int a=0, b=0, c=0, d=0;
sumbyref(i, j, ref a, ref b, ref c, ref d);
MessageBox.Show("sum=" + a.ToString() + "subtraction =" + b.ToString() + "  " +"multiplication=" + c.ToString() + " " + "division =" + d.ToString());
}


 private void button3_Click(object sender, EventArgs e)
{

char[] ch = textBox1.Text.ToCharArray();
{
int i = ch.Length;
MessageBox.Show(ch[0].ToString() + ch[i - 1].ToString());
}
}
}
}





0 comments:

Post a Comment