- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
Admin
Admin
Good. Let us know when you are done, then maybe you can spend 5 minutes reading http://www.dotnetperls.com/return. It teaches about the 'return' statement.
Admin
Beg to differ - I sympathise completely with the company (whether it's a real posting from them or not).
We used to do similar things with our school text books when I was about 12 or 13 or so. It was mildly amusing to have been reminded about what naughtly little tykes we were back then, but the joke's a bit tired now.
Admin
If you bothered to find out how to spell "moron" then maybe you wouldn't be treated like one, shithead.
Admin
A British comic book.
Admin
WHOOOOOOOSH!~
got one!
You also failed to note that I am the person treating others like morans. I guess comprehension isn't your strong suit.
Admin
Like I said, "shithead".
Admin
You add a lot to the thread. Get a brain moran.
Admin
javascript:void(window.open("data:text/plain;base64,"+window.getSelection(),null,"width=500,height=300"))
Admin
I'm amazed that none of you smart guys realised it's Schrödinger's Cat in a computer program: By observing the object, you affect it....
Admin
So... apparently the right people read these forums after all.
Admin
Admin
sigh
Admin
So an expression with multiple '=' is evaluated right to left....
The point is that '=' assigns the value on the right to the value on the left. This means that it must evaluate the right side first. It isn't a result of the assignment per se'...
when we say :
return a = 5;
we aren't saying "return the result of a = 5, but rather "return a after executing a = 5"
Admin
Admin
Admin
Admin
I've never understood what people who think others shouldn't be posting are actually doing reading the comments. If you understand everything perfectly, and aren't wanting to contribute any pearls of wisdom other than to attack anyone who does. Oh, maybe you don't have a life...
nevermind!
Admin
I wonder if I'm about to reach a "I know you are, but what am I"....
Admin
Maybe he didn't understand the problem half as well as he thought he did. Sorry that was harsh, he probably did understand the problem half as well as he thought he did.
Admin
In fact, it is possible to see that, to the extent I see a difference between the two explanations, it really is accurate to say "return the result of a=5" and "the result of a=5 is the value assigned to a", and not "the result of a=5 is the value of a". (It would also be more accurate to say "return 5 after executing a=5".)
Take the following C# code:
Method 'foo' disassembles (when compiled with MSVC 2010 in release mode) to the following MSIL:
The instruction at IL_0001 is doing the evaluation of the right side of the assignment (we'll come back to that later). The instruction at IL_0002 is duplicating the result of that evaluation -- the topmost instance is going to be returned (after being stored in local 0 and reloaded later), and the second one is going to be assigned to 'a'. IL_0003 stores that result to be returned later, and IL_0006 stores it to 'a'. IL_000b re-loads the result so that it can be returned.
The thing to note is that even though 'a' is volatile, it does not reload it (there is no ldfld instruction). An assignment really does evaluate to the value that was assigned.
'=' is like C's ',' operator, except with one additional side effect.
Admin
Thus, when faced with:
it is more helpful to picture it as:
than as
Although all 3 examples return the same value (and probably in a near identical way), the 2nd example is the one that (again IMO) best describes what is happening.
The original example is confusing because some other languages (apparently) return something else (a boolean, I guess they mean) (I think it's more likely that some programmers think of it that way rather than it actually happens that way).
Bit like using parenthesis for every loop/if/case.... Sometimes intentions can get obfuscated when you try to be too succinct.
[footnote] Some of the surprise was apparently coming from C++ programmers. C++ (tested with a MinGW compiler) behaves the same way (as does C), and although I haven't a java compiler immediately handy I suspect Java would too..... [/footnote]
Admin
Hey, i love sugar.
Admin
I think that this is a antistandard. You have to put an 'm' before field member, or put an underscore to be in standard. But if your property are simple getter and setter, then you use autoimplemented properties, such as
public int x { get; set; }
Admin
Part of the definition of the in-fix assignment function in C family of languages is that the return value of that function is the evaluated RHS.
eg return a>5 is not
a>5; return a;
it is evaluate (a>5) and return the answer.
Similarly return a=5 is evaluate (a=5) [which gives 5], then return the answer.
Admin
return a=5; and instinctively think it is a comparison operation, not an assignment operation.
You may claim that each of the examples I provided are equivalent, and functionally, they probably are - the point I was trying to make was that it is more readable (IMO - admittedly) to assign then return in this context.
Assuming you are correct that assignment is defined to return the RHS (I've not found anything to confirm that - although having not looked very hard, I've not found anything against that, either) still doesn't make my suggestion wrong.
Sure, it means that you can return and assign in one statement - this I have agreed with, in each post - my point was (meant to be) that it is not the clearest way to do it.
Admin
You should probably think of it more like this:
tmp = (a>5); return tmp;
This still works for the original case:
tmp = (a=5); return tmp;
In either case, you can treat is as if the operation is performed, the result assigned to a temporary variable, and that temporary is returned.
Each programming language has its own definition of what the 'return value' of an assignment operation is (i.e. what it is that's getting assigned into tmp in the example above).
In C, the 'return value' is the value of the variable to the left of the assignment operator (the l-value).
In C++, primitive types (int, long, double, etc) act as they do in C. For struct/class assignments, it's up to the implementation of operator=. The default auto-generated operator= usually returns a reference to the l-value.
In C# it's the value to the right of the assignment operator. This is spelled out in the MSDN documentation for C#'s = operator, and EvanED's example shows this in action.
Admin
Think of it like a function call. On one hand, function calls are sort of "special" expressions too -- you can make them do all sorts of stuff. They can do file I/O, they can write to global variables, make network requests, etc. In that respect, function calls are like '=', because they (can) have side effects. But they can also return a value, and you can use that value later.
If I say
, I'd bet you don't conceptually think of that as me saying Oh, on that you're right. Assignment-in-a-larger-expression (i.e. more than just 'x = 5;' or perhaps 'a = b = c = 5;') is something that I think should be pretty largely eschewed.I feel pretty similarly about i++ and ++i; I almost never like having those as part of a large expression. (The fact that some people think that while(*p++ = *q++); is something actually reasonable to write drives me batty.)
The one place where I'll sometimes use it is if the alternative is a loop with a break. I will sometimes write something like
in preference to
That use is reasonably idiomatic and I don't feel too bad about it, though I certainly don't insist that it's better.
But, the point stands: 'a=5' does evaluate to a value (5), even if it's not immediately obvious. And covering your ears and going LA-LA-LA-LA-I-CAN'T-HEAR-YOU at your programming language is rarely a good technique, even if it's trying to tell you that it's doing something obnoxious. :-)
I'm what some people may call a "language lawyer", so I'll stick true to my form:"There are several assignment operators, all of which group right-to-left. ... The result of the assignment operation is the value stored in the left operand after the assignment has taken place; the result is an lvalue." (C++03 standard, 5.17 para 3.)
Admin
"grown-up place" followed by "faggoty square" ... "out of touch" ... "stupid" ... "go get fucked".
Congratulations on winning today's dissonance award.
Admin
Aahh. Memories of "Buster Gonad and his unfeasibly large testicles" :-)
Admin
I apologise for being wrong saying that the result is from the right side in my initial post. In my defense, I don't make my living as a coder, I'm just a dabbler.
Admin
Wow. It's a sort of a Schrodinger's property. Until you look at it, the value can be anything. But the moment you look, it's 5...
Admin
Yes - but that's only for large values of 2...
Admin
Also, everyone on this thread except those who got it right are stupid.
Admin
Admin
Not everybody who visits this forum is completely au fait with the syntax of every programming language, whether it be C# or not. To suggest that, because they express confusion and uncertainty about a seriously non-standard usage of such a language, they are by definition idiots, indicates a level of intellectual arrogance and provincialism more usually seen in fundamentalist religious communities.
IMO it was perfectly appropriate for someguy to demonstrate what it does rather than what everyone else was doing, arguing about what they think it ought to have been doing, and thereby cut through all the confusion.
I can quite understand why your attitude appears to have caused anger amongst some of the respondents. As for me, if you had spoken to me like that in a meeting, for example, you would be clearing your desk and returning your door pass as I type.
Admin
If you're going to call people stupid, at least get your own statement right.
does NOT assign 3 to a. (a = 5) is not a variable, and the left hand side of an assignment has to be a variable. This won't even compile, never mind assign anything.Straight from the C# help:
So the right hand side of the = is being both set into the left, and returned as a value. So the most accurate way to picture it is that
is about the same asIt isn't a that is being returned, it is the value of the assignment.
Admin
You are clearly the moron here. For one thing, you've managed to spell it incorrectly 4 times at least, and twice after it was already pointed out that you were doing it wrong.
The fact that you now know that you are incorrect, but are choosing to continue to spell it wrong demonstrates exactly what kind of loser you are - one who, instead of graciously conceding a point will argue ad absurdam.
Why not take on board the fact that this site is about both fun and learning. The idea is that people who may not be quite as experienced as others can learn from the funny mistakes that have been made.
If you think that people who can't code C# as well as you shouldn't be here, well you're just plain wrong. There are many people here who are simply new to the industry, and many who code in other languages. In order to join in, and learn, people make comments, sometimes demonstrating that they don't know quite as much as you do.
In this case you have 3 options:
Why is it that you always choose 3?
Admin
Admin
Hahaha!!! LOCK (this) //make it thread safe...
Oh dear! Someone needs to brush up on their thread safety, don't they???
Admin
Um, I think it was a joke. Hint: the "yhbt" parameter name in the performAction method.
If you need to be informed as to what "yhbt" means, then you might want to type it into a search engine. An example that many people use is Google, which can be found at www dot google dot co dot uk (I assume you're British, from your username).
Admin
so many of these comments can be replied with "whoosh" or "Whoooooooosh !"
Admin
Admin
Admin
quite... ( (a=5) == 5 )
Admin
That's what architects do.
Admin
Admin
Also, at least in C++-land, that causes undefined behavior, so you won't necessarily get 3 out of it.
Admin
so much fail in this thread