Reputation: 351
I tested the following programs:
public class test
{
public string phone;
}
public class verify
{
private string _phoneNo;
public string phoneNo
{
get { return _phoneNo; }
set { _phoneNo = !String.IsNullOrEmpty(value) ? "*" : ""; }
}
}
class Program
{
static void Main(string[] args)
{
verify verify1 = new verify();
test test1 = new test { phone = verify1.phoneNo = "This is a phone number" };
Console.WriteLine("test.phone is: " + test1.phone);
Console.WriteLine("verify1.phoneNo is: " + verify1.phoneNo);
Console.ReadLine();
}
}
and got the result:
test.phone is: This is a phone number
verify1.phoneNo is: *
I feel confused about the result of "test.phone"
. Should it be "*"
as verify1.phoneNo?
Should phone = verify1.phoneNo = "This is a phone number"
be equal to
phone = (verify1.phoneNo = "This is a phone number");
Upvotes: 4
Views: 192
Reputation: 203823
An assignment expression evaluates to the value assigned to the variable. This is different from the second operand (as an implicit conversion may be performed before the assignment can be made, although that's not going on here) and it may be different from the value returned by evaluating the variable again (because it may not store exactly the value assigned to it).
In this case the code:
verify1.phoneNo = "This is a phone number"
resolves to "This is a phone number"
(which is what is then assigned to phone
) even though "*"
is what ends up being stored in the backing field for the phoneNo
variable.
Should
phone = verify1.phoneNo = "This is a phone number"
be equal tophone = (verify1.phoneNo = "This is a phone number");
They are equal. The parenthesis aren't changing the order of evaluation here.
Upvotes: 4