Multiple Assignment (Field = Property = Value)

Is it safe to do this in C #?

field = Property = value; 

It is guaranteed that the setter and receiver will be called sequentially and only the result of the receipt will be assigned to the field , and not necessarily value ? Will the compiler optimize it only to value ?

+6
source share
1 answer

Using

  private int tada; public int TADA { get { Console.WriteLine("GETTER"); return tada; } set { Console.WriteLine("SETTER"); tada = value; } } 

and

  int s = TADA = 1; 

I only get SETTER, which is written to the output window, so it does not call the receiver.

From C # Language Basics

You can even assign one value to several variables, for example:

int a, b, c, d;

a = b = c = d = 5;

In this case, the values โ€‹โ€‹of a, b, c and d will be 5. This works because the C # compiler performs the rightmost assignment; that is, d = 5. This assignment itself returns a value, a value of 5. the compiler then assigns this return value c. This second task also returns a value, etc., until all variables have been assigned.

+4
source

Source: https://habr.com/ru/post/947818/


All Articles