Topic: Ref and Out Keywords
Example-1:
private void button1_Click(object sender, EventArgs e)
{
int Age = 18;
Modify(Age);
MessageBox.Show(Age.ToString());
}
void Modify(int Age)
{
Age += 2;
}
Result: 18
Example-2: Ref Keyword
private void button1_Click(object sender, EventArgs e)
{
int Age = 18;
Modify(ref Age);
MessageBox.Show(Age.ToString());
}
void Modify(ref int Age)
{
Age += 2;
}
Result: 20
Example-3:
private void button1_Click(object sender, EventArgs e)
{
string name=”nothing”;
Modify(name);
MessageBox.Show(name);
}
void Modify(string name)
{
name = "SDU";
}
Result: nothing
Example-4: Out Keyword
private void button1_Click(object sender, EventArgs e)
{
string name=”nothing”;
Modify(out name);
MessageBox.Show(name);
}
void Modify(out string name)
{
name = "SDU";
}
Result: SDU
Переглядів: 163 |