472,794 Members | 4,045 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Guid readonly?

I guess I am forgetting some rules, why I am not able to do following, says it is read only --
Guid[] a = new Guid[2];
foreach(Guid b in a)
b = Guid.Empty;

but can do as following --

int c = 0;
while(c < a.Length)
a[c++] = Guid.Empty;

Dec 25 '05 #1
8 3052
KJ
This has to do with the restrictions applicable to your variable ("b")
in the foreach statment: System.Guid is a value-type (a struct),
therefore, it is read-only. Cf. "foreach, in" in MSDN (C# Programmer's
Reference).

Dec 25 '05 #2
Collection (and possibly arrays) cannot be changed when using the foreach
construct.

When foreach is used, it locks the objects to ensure that the information
returned from the enumerator is consistent and does not change. Although
your scenario probably will not cause the enumerator to get confused, it
locks it so that you cannot add or remove elements in something like an
array list.

"Pohihihi" <no*****@hotmail.com> wrote in message
news:OJ**************@TK2MSFTNGP15.phx.gbl...
I guess I am forgetting some rules, why I am not able to do following, says
it is read only --
Guid[] a = new Guid[2];
foreach(Guid b in a)
b = Guid.Empty;

but can do as following --

int c = 0;
while(c < a.Length)
a[c++] = Guid.Empty;
Dec 25 '05 #3
It did not come in my mind to check foreach docs for this. Yes, that is that
case, I read it just now.
Thx.

"KJ" <n_**********@mail.com> wrote in message
news:11*********************@g47g2000cwa.googlegro ups.com...
This has to do with the restrictions applicable to your variable ("b")
in the foreach statment: System.Guid is a value-type (a struct),
therefore, it is read-only. Cf. "foreach, in" in MSDN (C# Programmer's
Reference).

Dec 25 '05 #4
Actually, the value type is not read only. What happens in this
situation is that in the foreach statement, b is a copy of the Guid from a.
This is due to the assignment semantics on value types, where it is copied
on assignment. The reason why you can make the assignment in the for loop
is because the loop is accessing the array directly.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"KJ" <n_**********@mail.com> wrote in message
news:11*********************@g47g2000cwa.googlegro ups.com...
This has to do with the restrictions applicable to your variable ("b")
in the foreach statment: System.Guid is a value-type (a struct),
therefore, it is read-only. Cf. "foreach, in" in MSDN (C# Programmer's
Reference).

Dec 26 '05 #5
Nicholas Paldino [.NET/C# MVP] <mv*@spam.guard.caspershouse.com> wrote:
Actually, the value type is not read only.


I'm not sure what you mean by this, but the variable itself is
certainly read only - and if you were able to directly set any fields
within the struct normally, they would be read only too. Here's an
example:

using System;

struct Foo
{
public int x;
}

class Test
{
static void Main()
{
Foo[] f = new Foo[10];
foreach (Foo foo in f)
{
foo.x = 10;
foo = new Foo();
}
}
}

Both of the assignments fail - foo.x isn't classified as a variable
because foo is readonly, and the direct assignment fails for the same
reason.

From the spec:

<quote>
In either expansion, the enumerator variable is a temporary variable
that is inaccessible in, and invisible to, the embedded statement, and
the element variable is read-only in the embedded statement.
</quote>

Note the last phrase.

Your post gave the reason why if it *did* compile, it still wouldn't do
what Pohihihi wanted - but the above is why it didn't compile in the
first place.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Dec 26 '05 #6
Peter Rilling <pe***@nospam.rilling.net> wrote:
Collection (and possibly arrays) cannot be changed when using the foreach
construct.

When foreach is used, it locks the objects to ensure that the information
returned from the enumerator is consistent and does not change. Although
your scenario probably will not cause the enumerator to get confused, it
locks it so that you cannot add or remove elements in something like an
array list.


I don't think that's quite the reason. I believe the reason is that if
the foreach variable weren't read-only, people might assume that if
they changed the value, the value within the collection would change -
which it wouldn't.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Dec 26 '05 #7
Pohihihi stated:

System.Guid is a value-type (a struct), therefore, it is read-only

Implying that value types are read only by default. I should have said
"value types" and not "a value type". It was a comment about the other
comment about the inherent nature of value types, not whether or not it was
read-only due to the nature of it being a value type.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Nicholas Paldino [.NET/C# MVP] <mv*@spam.guard.caspershouse.com> wrote:
Actually, the value type is not read only.


I'm not sure what you mean by this, but the variable itself is
certainly read only - and if you were able to directly set any fields
within the struct normally, they would be read only too. Here's an
example:

using System;

struct Foo
{
public int x;
}

class Test
{
static void Main()
{
Foo[] f = new Foo[10];
foreach (Foo foo in f)
{
foo.x = 10;
foo = new Foo();
}
}
}

Both of the assignments fail - foo.x isn't classified as a variable
because foo is readonly, and the direct assignment fails for the same
reason.

From the spec:

<quote>
In either expansion, the enumerator variable is a temporary variable
that is inaccessible in, and invisible to, the embedded statement, and
the element variable is read-only in the embedded statement.
</quote>

Note the last phrase.

Your post gave the reason why if it *did* compile, it still wouldn't do
what Pohihihi wanted - but the above is why it didn't compile in the
first place.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Dec 26 '05 #8
KJ
I'm sorry, but this seems to me a deliberate misread of my post. When
did I say that value types are read-only by default? When did I comment
on "the inherent nature of value types?" Value-types are read-only in
the context I was referring to them in, that is all.

Dec 26 '05 #9

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

Similar topics

4
by: Louis Frolio | last post by:
Greetings All, I have read many upon many articles here regarding GUID data types and uniqueness. There have been many opinions regarding the effectiveness of GUID's and when they should/should...
3
by: ME | last post by:
I am developing a kind of plugin framework that I can use for several applications I want to make. In order to identify individual plugins I am currently doing something like the code below...
2
by: Brent Horine | last post by:
Still a newbie to C#. How do I declare a Guid as a constant? public const Guid GUID_BLUETOOTH_HCI_EVENT = new Guid(0x850302a, 0xb344, 0x4fda, 0x9b, 0xe9, 0x90, 0x57, 0x6b, 0x8d, 0x46, 0xf0); ...
2
by: Tomas Larsson | last post by:
Hi. I have some problems when using a static readonly declaration of a guid in a switch/case statement. I'll give you an example. public sealed class Activites { private Activites(){}
4
by: PawelF | last post by:
I need to replace string with Guid ready to use: public const string GUID_1 = "CF0003D61F6E4D6AA6B17B18E5057FCD"; like public const Guid ... Is there any chance to do this.
7
by: ESPNSTI | last post by:
Hi, I'd like to create a Guid constant and the following doesn't work (Cannot implicitly convert type 'string' to 'System.Guid') : public const Guid MyGuid =...
5
by: rcolby | last post by:
Evening, Wondering if someone can point me in the right direction, on how I would compare a system.guid with a system.byte. system.guid (pulled from sql server table with a data type of...
5
by: notregister | last post by:
Hi i have declared the following GUID Public myGUID3 As New Guid("00873FDF-61A8-11D1-AA5E-00C04FB1728B") when i run the program, during debugging, when i check the myGUID3 value, it return as...
1
by: Brian Henry | last post by:
I need to mark assemblies with a unique id (GUID in this case) and retrieve it when the assembly is loaded into my application (a plug-in loader)... the plug-in assembly all have a class in it...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.