Basic fundamental I messed up with C# Properties and reference types.
public Address adr { get; set; }
public class Address
{
//Field
public string AddressLine1;
//Property
public string Country {get; set;}
}
public void ChangeCountry(string _Country)
{
_Country = "US";
}
public void ChangeAddress(Address _adr)
{
_adr.Country = "US";
}
private void button1_Click(object sender, EventArgs e)
{
adr = new Address();
ChangeCountry(adr.Country);
MessageBox.Show(adr.Country);
//(?)Country is reported empty string. Property did not updated outside.
ChangeAddress(adr);
MessageBox.Show(adr.Country);
//OK. Country is reported 'US'
}
Here my questions;
When we pass a Property(simple or complex one) directly to a method that will attempt to modify it, Is it ensured this property modified everywhere? I mean Like the ref keyword? Since I can't use the "ref" keyword with properties;I directly pass property to a method for modification and I'm not sure if it is always going to be updated/modified also at outside. When I dont see ref keyword; it makes me worried :)
In the above example; adr class is updated successfully(ChangeAddress) without ref keyword; but the ChangeCountry method could not updated the string Country property. Is this a special case for strings?