I play the other day with some classes and i notice something:
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
public void pp_txt(ref TextBox txtTest)
{
txtTest.Text = "Test";
}
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2.pp_txt(ref textBox1);
}
}
and this:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2.pp_txt(textBox1);
}
}
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
public void pp_txt(TextBox txtTest)
{
txtTest.Text = "Test";
}
}
It looks like because TextBox is a class and the operator "=" is changed in C# you can pass it without using the "ref" operator for classes(if i recall correct in C++ you can do this only with pointers) and in the two examples the text in textbox will change. So this is how this looks to me and when almost everything is class type in C# why should we use ref(that is not for variables). To be honest i dont remember a clear example where to use ref. So why should we use ref when we can pass the classes without the ref operator?
This post has been edited by NoBrain: 10 February 2011 - 03:19 AM

New Topic/Question
Reply


MultiQuote






|