473,397 Members | 1,961 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,397 software developers and data experts.

C# compiler bug

The following program outputs False rather than True.
using System;

namespace Test

{

public struct V

{

public bool booleanV;

public void setBoolean(bool _value)

{

booleanV = _value;

}

}

public class C

{

public readonly V Value;

}

public class StructBug

{

static public void Main(string[] args)

{

C c = new C();

c.Value.setBoolean(true);

Console.WriteLine(c.Value.booleanV);

Console.Read();

}

}

}
Nov 16 '05 #1
9 1092
On Fri, 18 Jun 2004 14:23:48 -0700, "Subramaniyan Neelagandan"
<su***@neptune-tech.com> wrote:
The following program outputs False rather than True.


And that is correct. It's a programmer bug, not a compiler bug.
Investigate the difference between struct and class.
--
http://www.kynosarges.de
Nov 16 '05 #2
Subramaniyan Neelagandan <su***@neptune-tech.com> wrote:
The following program outputs False rather than True.


And indeed it should.

From the ECMA C# spec, section 14.5.4, Member Access:
(applicable sections only):

A member-access of the form E.I, where E is a primary-expression or a
predefined-type and I is an identifier, is evaluated and classified as
follows:

If E is a property access, indexer access, variable, or value, the type
of which is T, and a member lookup (§14.3) of I in T produces a match,
then E.I is evaluated and classified as follows:

If T is a class-type and I identifies an instance field of that class-
type:

[...]

Otherwise, if the field is readonly and the reference occurs outside an
instance constructor of the class in which the field is declared, then
the result is a value, namely the value of the field I in the object
referenced by E.
So, due to it being a read-only field, the result of the expression
c.Value is the value of c.Value, not a variable. Changing the data in
that value doesn't change the data in c.Value, as V is a value type.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #3
On Fri, 18 Jun 2004 22:42:44 +0100, Jon Skeet [C# MVP]
<sk***@pobox.com> wrote:
So, due to it being a read-only field, the result of the expression
c.Value is the value of c.Value, not a variable. Changing the data in
that value doesn't change the data in c.Value, as V is a value type.


You will also get a compiler error if you write the more obvious
operation: c.Value.booleanV = true;

Using a set method for a struct field is a rather ingenuous method to
sneak bugs into a C# program, I have to admit... maybe the next
version of the C# compiler could check for that?
--
http://www.kynosarges.de
Nov 16 '05 #4
Christoph Nahr <ch************@kynosarges.de> wrote:
The following program outputs False rather than True.


And that is correct. It's a programmer bug, not a compiler bug.
Investigate the difference between struct and class.


Well, in particular, how that interacts with the fact that the Value
variable is readonly. If you make it writable, it prints True.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #5
Hi,

Which spec you are talking about? I have version 1.2 in word format as well
as in my MSDN library. Both are talking about Enum.

So, could you point me to the spec you are talking about?

Thanks
SN
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Subramaniyan Neelagandan <su***@neptune-tech.com> wrote:
The following program outputs False rather than True.


And indeed it should.

From the ECMA C# spec, section 14.5.4, Member Access:
(applicable sections only):

A member-access of the form E.I, where E is a primary-expression or a
predefined-type and I is an identifier, is evaluated and classified as
follows:

If E is a property access, indexer access, variable, or value, the type
of which is T, and a member lookup (§14.3) of I in T produces a match,
then E.I is evaluated and classified as follows:

If T is a class-type and I identifies an instance field of that class-
type:

[...]

Otherwise, if the field is readonly and the reference occurs outside an
instance constructor of the class in which the field is declared, then
the result is a value, namely the value of the field I in the object
referenced by E.
So, due to it being a read-only field, the result of the expression
c.Value is the value of c.Value, not a variable. Changing the data in
that value doesn't change the data in c.Value, as V is a value type.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #6
Hi,

I found the section. It is 7.5.4.

Thanks

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Subramaniyan Neelagandan <su***@neptune-tech.com> wrote:
The following program outputs False rather than True.


And indeed it should.

From the ECMA C# spec, section 14.5.4, Member Access:
(applicable sections only):

A member-access of the form E.I, where E is a primary-expression or a
predefined-type and I is an identifier, is evaluated and classified as
follows:

If E is a property access, indexer access, variable, or value, the type
of which is T, and a member lookup (§14.3) of I in T produces a match,
then E.I is evaluated and classified as follows:

If T is a class-type and I identifies an instance field of that class-
type:

[...]

Otherwise, if the field is readonly and the reference occurs outside an
instance constructor of the class in which the field is declared, then
the result is a value, namely the value of the field I in the object
referenced by E.
So, due to it being a read-only field, the result of the expression
c.Value is the value of c.Value, not a variable. Changing the data in
that value doesn't change the data in c.Value, as V is a value type.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #7
Subramaniyan Neelagandan <su***@neptune-tech.com> wrote:
Which spec you are talking about? I have version 1.2 in word format as well
as in my MSDN library. Both are talking about Enum.

So, could you point me to the spec you are talking about?


The ECMA one. See http://www.ecma-international.org/ or
http://www.jaggersoft.com/csharp_standard/index.htm

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #8
Also if I strongly name an assembly, will it reference the recompiled
component once it's recompiled, or will it always keep the one its reference
was initially set to?

"Christoph Nahr" <ch************@kynosarges.de> wrote in message
news:3d********************************@4ax.com...
On Fri, 18 Jun 2004 14:23:48 -0700, "Subramaniyan Neelagandan"
<su***@neptune-tech.com> wrote:
The following program outputs False rather than True.


And that is correct. It's a programmer bug, not a compiler bug.
Investigate the difference between struct and class.
--
http://www.kynosarges.de

Nov 16 '05 #9
I don't believe that this has something to do with strong name or not.
But if you've declared the variable as const then the constant would be
hardbakened into all referencing assembly and changing itf value when
recompiling would not affect other assemblies.
But if you have readonly variables, changing them changes them in all
referencing assemblies.

--
cody

[Freeware, Games and Humor]
www.deutronium.de.vu || www.deutronium.tk
"Beeeeeves" <beeeeeeeeev@ves> schrieb im Newsbeitrag
news:OK**************@TK2MSFTNGP10.phx.gbl...
Also if I strongly name an assembly, will it reference the recompiled
component once it's recompiled, or will it always keep the one its reference was initially set to?

"Christoph Nahr" <ch************@kynosarges.de> wrote in message
news:3d********************************@4ax.com...
On Fri, 18 Jun 2004 14:23:48 -0700, "Subramaniyan Neelagandan"
<su***@neptune-tech.com> wrote:
The following program outputs False rather than True.


And that is correct. It's a programmer bug, not a compiler bug.
Investigate the difference between struct and class.
--
http://www.kynosarges.de


Nov 16 '05 #10

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

Similar topics

2
by: Jeff Epler | last post by:
Hello. Recently, Generator Comprehensions were mentioned again on python-list. I have written an implementation for the compiler module. To try it out, however, you must be able to rebuild...
13
by: Bryan Parkoff | last post by:
You may notice that switch (...) is much faster than function that can gain a big improved performance because it only use JMP instruction however function is required to use CALL, PUSH, and POP...
10
by: Bjorn | last post by:
I'm using interfaces in C++ by declaring classes with only pure virtual methods. If then someone wants to implement the interface they needs to inherit from the class. If the implementing class...
7
by: Tao Wang | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hi, I saw cuj's conformance roundup, but the result is quite old. I think many people like me want to know newer c++ standard conformance test...
14
by: joshc | last post by:
I'm writing some C to be used in an embedded environment and the code needs to be optimized. I have a question about optimizing compilers in general. I'm using GCC for the workstation and Diab...
16
by: pj | last post by:
(Was originally, probably wrongly, posted to the vc subgroup.) (This doesn't appear to be a c# problem, but a problem with a bug in the Visual Studio c# compiler, but, any help will be welcome...)...
0
by: rollasoc | last post by:
Hi, I seem to be getting a compiler error Internal Compiler Error (0xc0000005 at address 535DB439): likely culprit is 'BIND'. An internal error has occurred in the compiler. To work around...
3
by: Mark Rockman | last post by:
------ Build started: Project: USDAver2, Configuration: Debug .NET ------ Preparing resources... Updating references... Performing main compilation... error CS0583: Internal Compiler Error...
6
by: toton | last post by:
Hi, Anyone have a link to comparative study of different C++ compilers and how much they conform to C++ language standard? Most of the big platforms I know have GCC which well supports C++...
41
by: Miroslaw Makowiecki | last post by:
Where can I download Comeau compiler as a trial version? Thanks in advice.
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
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...
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...

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.