C# Language Spec (7.13.2)

R

Rene

The section 7.13.2 (Compound assignment) of the C# language specification
has the following paragraph:

The term "evaluated only once" means that in the evaluation of x op y, the
results of any constituent expressions of x are temporarily saved and then
reused when performing the assignment to x. For example, in the assignment
A()[B()] += C(), where A is a method returning int[], and B and C are
methods returning int, the methods are invoked only once, in the order A, B,
C.

I have been trying to figure out what a hell does A()[B()] += C() means and
create a sample program in C# that will show what this being applied in
practice but I CAN'T figure it out!!!

Could someone please post some sample code that describes this darn
concept?? Please, I am desperate!

Thanks.
 
R

Robert Jordan

Hi,
I have been trying to figure out what a hell does A()[B()] += C() means and
create a sample program in C# that will show what this being applied in
practice but I CAN'T figure it out!!!

Could someone please post some sample code that describes this darn
concept?? Please, I am desperate!

Cool it :)

int[] array = new int[] { 1, 2, 3};

int[] A() {
return array;
}

int B() {
return 2;
}

int C() {
return 3;
}

void Test() {
int[] a = A();

Dump(a); // -> 1, 2, 3

A()[B()] += C();

Dump(a); // -> 1, 2, 6
}

void Dump(int[] a) {
for (int i = 0; i < a.Length; i++) {
Console.WriteLine(a);
}
}



A()[B()] += C() is equivalent to:

int[] a = A();
int index = B();
int increment = C();
a[index] += increment;


bye
Rob
 
R

Rene

So that's what it was. Thanks for the example!



Now I only have to figure out what "evaluated only once" means because I see
the example but I don't see what is so particular about it. hmmmmmm........
what in the world does this means??? << head scratching >>



Robert Jordan said:
Hi,
I have been trying to figure out what a hell does A()[B()] += C() means
and create a sample program in C# that will show what this being applied
in practice but I CAN'T figure it out!!!

Could someone please post some sample code that describes this darn
concept?? Please, I am desperate!

Cool it :)

int[] array = new int[] { 1, 2, 3};

int[] A() {
return array;
}

int B() {
return 2;
}

int C() {
return 3;
}

void Test() {
int[] a = A();

Dump(a); // -> 1, 2, 3

A()[B()] += C();

Dump(a); // -> 1, 2, 6
}

void Dump(int[] a) {
for (int i = 0; i < a.Length; i++) {
Console.WriteLine(a);
}
}



A()[B()] += C() is equivalent to:

int[] a = A();
int index = B();
int increment = C();
a[index] += increment;


bye
Rob
 
R

Robert Jordan

Rene said:
So that's what it was. Thanks for the example!



Now I only have to figure out what "evaluated only once" means because I see
the example but I don't see what is so particular about it. hmmmmmm........
what in the world does this means??? << head scratching >>

Let us change the method B a little bit:

Random rng = new Random();

int B() {
return rng.Next(array.Length);
}

Now every time the method gets called it will return
a random number between 0 .. array.Length-1.

When the assignment A()[B()] += C() gets compiled into IL,
the compiler assures that B(), A(), C() are called
*exactly* once. If it wouldn't do that, your method B() will
return every time some other value and the computation
will be incorrect, like here:

int[] a = A();
int index = B();
a[B()] += C(); // B() is called again!

bye
Rob
 
G

Guest

Hi Robert,

Aha thx, finally I understeand this chapter too :) So following your last
example and give 2 lines of code that 'in fact' do the same:

// here a() and b() is called only 1 time
a[ b() ] += c();

// here a() and b() is called 2 time
a[ b() ] = a[ b() ] + c();

Is this correct ?

rgds, Wilfried
http://www.mestdagh.biz
 
R

Robert Jordan

Wilfried,
Aha thx, finally I understeand this chapter too :) So following your last
example and give 2 lines of code that 'in fact' do the same:

// here a() and b() is called only 1 time
a[ b() ] += c();

// here a() and b() is called 2 time
a[ b() ] = a[ b() ] + c();

Yes, but only in 'in fact' ;-)

bye
Rob
 
R

Rene

Brilliant!

Every once in a while I like to pretend that I am smart enough to
understand the C# language specs, reading the specs is like reading some
document put together by some crazy lawyer, and to make things worst, most
specs description have links other description that have links to other
description that have links to other description etc!!!

I am hoping that the more I read about the specs the more familiar I will
be come with the way they describe things and maybe even get better at my
English!

Thanks for your time.



Robert Jordan said:
Rene said:
So that's what it was. Thanks for the example!



Now I only have to figure out what "evaluated only once" means because I
see the example but I don't see what is so particular about it.
hmmmmmm........ what in the world does this means??? << head scratching

Let us change the method B a little bit:

Random rng = new Random();

int B() {
return rng.Next(array.Length);
}

Now every time the method gets called it will return
a random number between 0 .. array.Length-1.

When the assignment A()[B()] += C() gets compiled into IL,
the compiler assures that B(), A(), C() are called
*exactly* once. If it wouldn't do that, your method B() will
return every time some other value and the computation
will be incorrect, like here:

int[] a = A();
int index = B();
a[B()] += C(); // B() is called again!

bye
Rob
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top