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

A simple question about foreach and datatypes

Hello!

Why does not this cause a compile error because a ulong has never been
implicitly convertible to byte?

ulong[] vektor = {100000,200000,300000};
foreach(byte b in vektor)
{
Console.WriteLine("i={0}", b);
}

If I have these statements below then I get a compile error.
But it's almost the same as above. Is a bug perhaps?

ulong a = 1;
byte b;
b = a; // Here a ulong is not implicitly convertible to a byte so we get a
compile error which is correct

//Tony

Oct 19 '07 #1
7 1363
Your foreach loop instructions are basically translated by the C# compiler
into a bunch of other instructions that are understood by the CLR.

One of the instructions that is emitted on your behalf is an instruction to
convert ulong to a byte without throwing an overflow error.

If you are not ok with this behavior you can change it in code by adding the
check using word:

checked
{
ulong[] vektor = { 100000, 200000, 300000 };
foreach (byte b in vektor)
{
Console.WriteLine("i={0}", b);
}
}
"Tony Johansson" <jo*****************@telia.comwrote in message
news:KC*****************@newsb.telia.net...
Hello!

Why does not this cause a compile error because a ulong has never been
implicitly convertible to byte?

ulong[] vektor = {100000,200000,300000};
foreach(byte b in vektor)
{
Console.WriteLine("i={0}", b);
}

If I have these statements below then I get a compile error.
But it's almost the same as above. Is a bug perhaps?

ulong a = 1;
byte b;
b = a; // Here a ulong is not implicitly convertible to a byte so we get a
compile error which is correct

//Tony

Oct 19 '07 #2
Hello!

Why does not this generate a compile error?
That's my question.
What is the reason for the compiler to accept this type of conversion.

//Tony

"Rene" <a@b.comskrev i meddelandet
news:el**************@TK2MSFTNGP06.phx.gbl...
Your foreach loop instructions are basically translated by the C# compiler
into a bunch of other instructions that are understood by the CLR.

One of the instructions that is emitted on your behalf is an instruction
to convert ulong to a byte without throwing an overflow error.

If you are not ok with this behavior you can change it in code by adding
the check using word:

checked
{
ulong[] vektor = { 100000, 200000, 300000 };
foreach (byte b in vektor)
{
Console.WriteLine("i={0}", b);
}
}
"Tony Johansson" <jo*****************@telia.comwrote in message
news:KC*****************@newsb.telia.net...
>Hello!

Why does not this cause a compile error because a ulong has never been
implicitly convertible to byte?

ulong[] vektor = {100000,200000,300000};
foreach(byte b in vektor)
{
Console.WriteLine("i={0}", b);
}

If I have these statements below then I get a compile error.
But it's almost the same as above. Is a bug perhaps?

ulong a = 1;
byte b;
b = a; // Here a ulong is not implicitly convertible to a byte so we get
a
compile error which is correct

//Tony


Oct 19 '07 #3
As was explained before, the foreach loop is translated into this
(something like it):

using (IEnumerator enumerator = vektor.GetEnumerator())
{
while (enumerator.MoveNext())
{
byte b = (byte)enumerator.Current;
Console.WriteLine("i={0}", b);
}
}

There is an explicit (not implicit) cast that will succeed in your
case. However, the error, if there is one, will only occur at runtime.

The reason it succeeds in the generated code and not your code is that
the compiler puts some extra care into it to make sure it does
compile. Your example does not provide an explicit cast, while the
generated version does.

While I cannot send you the exact generated code (it is in MSIL), just
know that it does something similar to my example.

Oct 19 '07 #4
Why does not this generate a compile error?
That's my question.
For the same reason code below does not generate an error.

ulong a = 1;
byte b;
b = Convert.ToByte(a);

There is nothing wrong with code above, in that snippet, you are stating
that you want to convert a ulong to a byte and not to trhow an error if the
conversion overflows. why would the compiler throw a compile error here??

Like I said before, internally, the C# compiler is taking your foreach code
and creating code that the CLR can understand, **this icludes** generating
code that is doing the **convertion without trhouwing an error**, something
similar to the example avobe.

What is the reason for the compiler to accept this type of conversion.
Ah, because when you do a foreach loop internally you are calling the
GetEnumerator() function wich in turn return an IEnumerator that in turn
returns objects as it collection member, so there is no way to do a compile
time check because the compiler does not whats inside the object. You can
check the definition of the IEnumerator for more info.

If you want to loop throw an array and have the compile time check then do a
"for" loop.

ulong[] vektor = { 100000, 200000, 300000 };
for (int i = 0; i < vektor.Length; i++)
{
byte b = vektor[i];
Console.WriteLine("i={0}", b);
}


>
//Tony

"Rene" <a@b.comskrev i meddelandet
news:el**************@TK2MSFTNGP06.phx.gbl...
>Your foreach loop instructions are basically translated by the C#
compiler into a bunch of other instructions that are understood by the
CLR.

One of the instructions that is emitted on your behalf is an instruction
to convert ulong to a byte without throwing an overflow error.

If you are not ok with this behavior you can change it in code by adding
the check using word:

checked
{
ulong[] vektor = { 100000, 200000, 300000 };
foreach (byte b in vektor)
{
Console.WriteLine("i={0}", b);
}
}
"Tony Johansson" <jo*****************@telia.comwrote in message
news:KC*****************@newsb.telia.net...
>>Hello!

Why does not this cause a compile error because a ulong has never been
implicitly convertible to byte?

ulong[] vektor = {100000,200000,300000};
foreach(byte b in vektor)
{
Console.WriteLine("i={0}", b);
}

If I have these statements below then I get a compile error.
But it's almost the same as above. Is a bug perhaps?

ulong a = 1;
byte b;
b = a; // Here a ulong is not implicitly convertible to a byte so we get
a
compile error which is correct

//Tony



Oct 19 '07 #5
Hello!!

I understand what you mean but I would be more happy if this would cause a
compile error.
I think that the compiler is doing a bad job hiding an important error when
using byte for an ulong.

There is certainly a good reson for why the compiler is doing a thing that
may seems bad.
//Tony

<je**********@gmail.comskrev i meddelandet
news:11**********************@q5g2000prf.googlegro ups.com...
As was explained before, the foreach loop is translated into this
(something like it):

using (IEnumerator enumerator = vektor.GetEnumerator())
{
while (enumerator.MoveNext())
{
byte b = (byte)enumerator.Current;
Console.WriteLine("i={0}", b);
}
}

There is an explicit (not implicit) cast that will succeed in your
case. However, the error, if there is one, will only occur at runtime.

The reason it succeeds in the generated code and not your code is that
the compiler puts some extra care into it to make sure it does
compile. Your example does not provide an explicit cast, while the
generated version does.

While I cannot send you the exact generated code (it is in MSIL), just
know that it does something similar to my example.

Oct 19 '07 #6
Hello!

Good explained thanks!

//Tony

"Rene" <a@b.comskrev i meddelandet
news:u1**************@TK2MSFTNGP05.phx.gbl...
>Why does not this generate a compile error?
That's my question.

For the same reason code below does not generate an error.

ulong a = 1;
byte b;
b = Convert.ToByte(a);

There is nothing wrong with code above, in that snippet, you are stating
that you want to convert a ulong to a byte and not to trhow an error if
the conversion overflows. why would the compiler throw a compile error
here??

Like I said before, internally, the C# compiler is taking your foreach
code and creating code that the CLR can understand, **this icludes**
generating code that is doing the **convertion without trhouwing an
error**, something similar to the example avobe.

>What is the reason for the compiler to accept this type of conversion.
Ah, because when you do a foreach loop internally you are calling the
GetEnumerator() function wich in turn return an IEnumerator that in turn
returns objects as it collection member, so there is no way to do a
compile time check because the compiler does not whats inside the object.
You can check the definition of the IEnumerator for more info.

If you want to loop throw an array and have the compile time check then do
a "for" loop.

ulong[] vektor = { 100000, 200000, 300000 };
for (int i = 0; i < vektor.Length; i++)
{
byte b = vektor[i];
Console.WriteLine("i={0}", b);
}


>>
//Tony

"Rene" <a@b.comskrev i meddelandet
news:el**************@TK2MSFTNGP06.phx.gbl...
>>Your foreach loop instructions are basically translated by the C#
compiler into a bunch of other instructions that are understood by the
CLR.

One of the instructions that is emitted on your behalf is an instruction
to convert ulong to a byte without throwing an overflow error.

If you are not ok with this behavior you can change it in code by adding
the check using word:

checked
{
ulong[] vektor = { 100000, 200000, 300000 };
foreach (byte b in vektor)
{
Console.WriteLine("i={0}", b);
}
}
"Tony Johansson" <jo*****************@telia.comwrote in message
news:KC*****************@newsb.telia.net...
Hello!

Why does not this cause a compile error because a ulong has never been
implicitly convertible to byte?

ulong[] vektor = {100000,200000,300000};
foreach(byte b in vektor)
{
Console.WriteLine("i={0}", b);
}

If I have these statements below then I get a compile error.
But it's almost the same as above. Is a bug perhaps?

ulong a = 1;
byte b;
b = a; // Here a ulong is not implicitly convertible to a byte so we
get a
compile error which is correct

//Tony





Oct 19 '07 #7
JT
On Oct 19, 3:42 pm, "Tony Johansson" <johansson.anders...@telia.com>
wrote:
Hello!

Good explained thanks!

//Tony

"Rene" <a...@b.comskrev i meddelandetnews:u1**************@TK2MSFTNGP05.phx. gbl...
Why does not this generate a compile error?
That's my question.
For the same reason code below does not generate an error.
ulong a = 1;
byte b;
b = Convert.ToByte(a);
There is nothing wrong with code above, in that snippet, you are stating
that you want to convert a ulong to a byte and not to trhow an error if
the conversion overflows. why would the compiler throw a compile error
here??
Like I said before, internally, the C# compiler is taking your foreach
code and creating code that the CLR can understand, **this icludes**
generating code that is doing the **convertion without trhouwing an
error**, something similar to the example avobe.
What is the reason for the compiler to accept this type of conversion.
Ah, because when you do a foreach loop internally you are calling the
GetEnumerator() function wich in turn return an IEnumerator that in turn
returns objects as it collection member, so there is no way to do a
compile time check because the compiler does not whats inside the object.
You can check the definition of the IEnumerator for more info.
If you want to loop throw an array and have the compile time check then do
a "for" loop.
ulong[] vektor = { 100000, 200000, 300000 };
for (int i = 0; i < vektor.Length; i++)
{
byte b = vektor[i];
Console.WriteLine("i={0}", b);
}
//Tony
"Rene" <a...@b.comskrev i meddelandet
news:el**************@TK2MSFTNGP06.phx.gbl...
Your foreach loop instructions are basically translated by the C#
compiler into a bunch of other instructions that are understood by the
CLR.
>One of the instructions that is emitted on your behalf is an instruction
to convert ulong to a byte without throwing an overflow error.
>If you are not ok with this behavior you can change it in code by adding
the check using word:
>checked
{
ulong[] vektor = { 100000, 200000, 300000 };
foreach (byte b in vektor)
{
Console.WriteLine("i={0}", b);
}
}
>"Tony Johansson" <johansson.anders...@telia.comwrote in message
news:KC*****************@newsb.telia.net...
Hello!
>>Why does not this cause a compile error because a ulong has never been
implicitly convertible to byte?
>>ulong[] vektor = {100000,200000,300000};
foreach(byte b in vektor)
{
Console.WriteLine("i={0}", b);
}
>>If I have these statements below then I get a compile error.
But it's almost the same as above. Is a bug perhaps?
>>ulong a = 1;
byte b;
b = a; // Here a ulong is not implicitly convertible to a byte so we
get a
compile error which is correct
>>//Tony- Hide quoted text -

- Show quoted text -
I was looking at the VS options (Tools... Options...). I found a
setting for VB in Projects and Solutions, Option Strict, that when
checked will require explicit conversions, but I didn't see anything
for C#. However, I certainly wouldn't advise anyone to use VB because
of that. :^>

Since I haven't changed project Build settings from the defaults, I
don't know if this would be caught or not, but you could probably go
into the Project Properties, the Build tab, and change settings to
give you more control over your error reporting. You might try
unchecking the Optimize code checkbox. Make sure Warning Level is set
to the highest number. Make sure the Suppress warnings text box is
clear. Under Treat warnings as errors, check the Specific warnings
radio button and specify a warning that you would want elevated to an
error. I don't know what that would be, but I think the Help files
might give you that.

JT

Oct 20 '07 #8

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

Similar topics

2
by: Ivan Demkovitch | last post by:
Hi! I'm trying to parse RSS news feed with code as simple as possible. Simple XML: <?xml version="1.0"?> <rss version="2.0"> <channel> <item>
3
by: Mark | last post by:
What are the best .NET datatypes to handle SQL Server's Float and Real datatypes? I'd like to avoid using the SQL Server specific datatypes like SqlInt32 or similar. Thanks in advance. -Mark
2
by: Mark Gibson | last post by:
Hello, I've been experimenting with dblink recently, and have encountered some limitations I'd like to discuss. I've been trying to create views of remote tables, like so: CREATE VIEW stuff...
73
by: Claudio Grondi | last post by:
In the process of learning about some deeper details of Python I am curious if it is possible to write a 'prefix' code assigning to a and b something special, so, that Python gets trapped in an...
5
by: michal | last post by:
hi guys, i thought you might be interested in a nice JSON class which converts ASP datatypes (basic datatypes, dictionaries, recordsets, ...) into JSON so that javascript can easily understand it...
9
by: news.microsoft.com | last post by:
I am looping through an iteration and I would like to test the next item but if its not the one that I want how do I put it back so that when my foreach continues it is in the next iteration? ...
6
by: SuperFool | last post by:
This has got to be one of those questions only a serious newbie would come up with.... Basicly: I select all the city names in the table and turn them into a pull down menu (code below) print...
6
by: Jeff | last post by:
Dear experts! ..NET 2.0 I'm trying to make an array containg multiple datatypes. This array will consist of 3 items (string, string, integer): my first try was this, (of course it fails)...
0
by: Tony Johansson | last post by:
Hello! I have the code below. In the previous code not shown below I have used linq to extract some row and these have been placed in the var variable result. In my code there is just one entry...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...

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.