473,503 Members | 1,858 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

What is this syntax???

double c = double.NaN;

Console.WriteLine(c = 10);
Console.WriteLine(c += 5);
Console.WriteLine(c);

output:
10
15
15

Question: Has it always been possible to do these assingments and
operations, and how can such an operations return a value?? I like it, but
thought that this kind of operations were nice candy back from my c++ days
and not allowed in c#.

regards Jesper.

Nov 4 '08 #1
6 1351
Jesper wrote:
double c = double.NaN;

Console.WriteLine(c = 10);
Console.WriteLine(c += 5);
Console.WriteLine(c);

output:
10
15
15

Question: Has it always been possible to do these assingments and
operations, and how can such an operations return a value?? I like it, but
thought that this kind of operations were nice candy back from my c++ days
and not allowed in c#.
What you have inside of the first two WriteLine() calls is an assignment
expression. So
c = 10
assigns 10 to c and evaluates to the assigned value, similar
c += 5
inrements c by 5 and evaluates to the assigned value.

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Nov 4 '08 #2
The logical result of any assignent is the value that was assigned. So
this behaves as expected. This also allows the abbreviated syntax:

obj.Foo = obj.Bar = true;

where true is assigned to Bar and Foo (Bar is not queried at any point;
the result of the first assignment (true) is used for the second
assignment).

Marc
Nov 4 '08 #3
Jesper wrote:
double c = double.NaN;

Console.WriteLine(c = 10);
Console.WriteLine(c += 5);
Console.WriteLine(c);

output:
10
15
15

Question: Has it always been possible to do these assingments and
operations, and how can such an operations return a value?? I like it, but
thought that this kind of operations were nice candy back from my c++ days
and not allowed in c#.
They are allowed in C#.

Your examples are not examples of where it makes sense.

But you will see the following a lot:

string line;
while((line = sr.ReadLine()) != null)
{
....
}

And it is using the same feature.

Arne
Nov 4 '08 #4
"Jesper, Denmark" <Je***********@discussions.microsoft.comwrote in message
news:06**********************************@microsof t.com...
Question: Has it always been possible to do these assingments and
operations, and how can such an operations return a value?? I like it, but
thought that this kind of operations were nice candy back from my c++ days
and not allowed in c#.
Quite the opposite; the usage is expanding: not only are they allowed in C#,
but as of the first version of VB.NET the syntax was added to that language
too!
Nov 4 '08 #5
VB has never allowed assignments within expressions.
--
http://www.tangiblesoftwaresolutions.com
C++ to C#
C++ to VB
C++ to Java
VB & C# to Java
Java to VB & C#
Instant C#: VB to C#
Instant VB: C# to VB
Instant C++: VB, C#, or Java to C++/CLI
"Jeff Johnson" wrote:
"Jesper, Denmark" <Je***********@discussions.microsoft.comwrote in message
news:06**********************************@microsof t.com...
Question: Has it always been possible to do these assingments and
operations, and how can such an operations return a value?? I like it, but
thought that this kind of operations were nice candy back from my c++ days
and not allowed in c#.

Quite the opposite; the usage is expanding: not only are they allowed in C#,
but as of the first version of VB.NET the syntax was added to that language
too!
Nov 4 '08 #6
"David Anton" <Da********@discussions.microsoft.comwrote in message
news:A6**********************************@microsof t.com...
Question: Has it always been possible to do these assingments and
operations, and how can such an operations return a value?? I like it,
but
thought that this kind of operations were nice candy back from my c++
days
and not allowed in c#.

Quite the opposite; the usage is expanding: not only are they allowed in
C#,
but as of the first version of VB.NET the syntax was added to that
language
too!
VB has never allowed assignments within expressions.
Sorry, I was only referring to the <operator>= syntax (for standalone
assignments). Obviously I was unclear.
Nov 4 '08 #7

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

Similar topics

28
3242
by: David MacQuigg | last post by:
I'm concerned that with all the focus on obj$func binding, &closures, and other not-so-pretty details of Prothon, that we are missing what is really good - the simplification of classes. There are...
37
2571
by: Bengt Richter | last post by:
ISTM that @limited_expression_producing_function @another def func(): pass is syntactic sugar for creating a hidden list of functions. (Using '|' in place of '@' doesn't change the picture...
70
8808
by: Roy Yao | last post by:
Does it mean "(sizeof(int))* (p)" or "sizeof( (int)(*p) )" ? According to my analysis, operator sizeof, (type) and * have the same precedence, and they combine from right to left. Then this...
121
9906
by: typingcat | last post by:
First of all, I'm an Asian and I need to input Japanese, Korean and so on. I've tried many PHP IDEs today, but almost non of them supported Unicode (UTF-8) file. I've found that the only Unicode...
8
4010
by: Hermawih | last post by:
Hello , I want your opinion about this . In order to say it clearly , I think I have to describe it in long sentences . I could consider myself as Intermediate/Advance Access Developer ;...
6
407
by: Daniel Rudy | last post by:
What is wrong with this program? When I try to compile it, I get the following error. Compiler is gcc on FreeBSD. strata:/home/dcrudy/c 1055 $$$ ->cc -g -oe6-3 e6-3.c e6-3.c: In function...
10
3194
by: Protoman | last post by:
Could you tell me what's wrong with this program, it doesn't compile: #include <iostream> #include <cstdlib> using namespace std; class Everything { public: static Everything* Instance()
3
16190
by: Manuel | last post by:
I'm trying to compile glut 3.7.6 (dowbloaded from official site)using devc++. So I've imported the glut32.dsp into devc++, included manually some headers, and start to compile. It return a very...
5
2375
by: Cylix | last post by:
this.menus = { root: new Array };
14
9817
by: gimme_this_gimme_that | last post by:
What is going on here with the dollar signs and parenthesis? $(document).ready(function(){ $("li").behavior("click",function(){ $(this).load(menu.html"); }); }); And when do you use click...
0
7202
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
7330
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6991
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...
1
5014
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...
0
4672
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3167
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3154
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1512
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
736
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.