473,406 Members | 2,356 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

C# Language Spec (7.13.2)

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.
Nov 16 '05 #1
6 1369
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[i]);
}
}

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

int[] a = A();
int index = B();
int increment = C();
a[index] += increment;
bye
Rob
Nov 16 '05 #2
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" <ro*****@gmx.net> wrote in message
news:cn*************@news.t-online.com...
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[i]);
}
}

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

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

Nov 16 '05 #3
Rene wrote:
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
Nov 16 '05 #4
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
Nov 16 '05 #5
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
Nov 16 '05 #6
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" <ro*****@gmx.net> wrote in message
news:cn*************@news.t-online.com...
Rene wrote:
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

Nov 16 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Ka | last post by:
I install a mysql server in default installation with latin charset, but I want to use GBK(a chinese charset), so that I can store and search chinese words directly. so, I download, unpack and...
63
by: Tristan Miller | last post by:
Greetings. Do any popular browsers correctly support <q>, at least for Western languages? I've noticed that Mozilla uses the standard English double-quote character, ", regardless of the lang...
5
by: Wladimir Borsov | last post by:
Assume I declare at the top of a HTML page: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> This refers to english HTML content/syntax. Later then I declare: <META...
2
by: Thomas G. Marshall | last post by:
Arthur J. O'Dwyer <ajo@nospam.andrew.cmu.edu> coughed up the following: > On Thu, 1 Jul 2004, Thomas G. Marshall wrote: >> >> Aside: I've looked repeatedly in google and for some reason cannot >>...
22
by: larry | last post by:
I was just looking at a demo training that mindleaders has on .net training: http://www.mindleaders.com/products/democourse3.asp And I don't believe this is correct or at least is misleading...
7
by: Edward Yang | last post by:
A few days ago I started a thread "I think C# is forcing us to write more (redundant) code" and got many replies (more than what I had expected). But after reading all the replies I think my...
3
by: Marshal | last post by:
/////////////////////////////////////////////////////////////////////////////////////////////// /// CONSTRAINTS ON GENERICS //////////////////////////////////////////////////// public class...
14
by: Magius | last post by:
Hello, I have a question about the correctness of the language grammar for the C# 2.0 specification. I am working with the grammar specified in the June 2005 ECMA-334 standard. Basically, a...
14
by: Robin Bowning | last post by:
Hi all, I have two questions I was hoping someone could answer regarding C#. First, I noticed C# does not allow local readonly variables. For example, the following is illegal: void X() {...
1
by: Tom Baxter | last post by:
Hi All, I was hoping someone could clarify something I read in the C# Language Spec regarding "Definite Assignment" of structs. This is a very subtle point. I am referring to ECMA-334, section...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.