Two equal (assign) statements on the same line

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 am confused by the result "test.phone". If it is equal "*"toverify1.phoneNo?

If it phone = verify1.phoneNo = "This is a phone number"will be equal  phone = (verify1.phoneNo = "This is a phone number");

+4
source share
1 answer

An assignment expression is evaluated by the value assigned to the variable. This is different from the second operand (because the implicit conversion can be performed before the assignment is done, although this does not happen here), and it can be different from the value returned by re-evaluating the variable (since it may not store the exact value assigned to it) .

In this case, the code:

verify1.phoneNo = "This is a phone number" 

"This is a phone number" ( , phone), "*" - , , phoneNo.

phone = verify1.phoneNo = "This is a phone number"  phone = (verify1.phoneNo = "This is a phone number");

. .

+4

All Articles