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

Why enums can convert to IntPtr?

Consider the following code snippet:

using System;
enum E { A, }
struct T
{
public static explicit operator T(int i) { return new T(); }
public static void Foo()
{
E e = 0;
IntPtr ptr = (IntPtr)e; // case 1: compiles fine
T t = (T)e; // case 2: gives an error in correspondence with the specs
}
}

Microsoft C# compiler gives an error in case 2 (which follows the specs). In
case 1 the code compiles fine.
Does the compiler follow the specs in case 1? If so, where is the
description of that case in the specs?

I used C# compiler from VS 2003 and 2.0 November preview.

Thanks,
andy
Nov 17 '05 #1
5 3188
I don't know but maybe a boxing case here.

--
Sylvain Lafontaine, ing.
MVP - Technologies Virtual-PC
E-mail: http://cerbermail.com/?QugbLEWINF
"Andrey Simanovsky" <An**************@discussions.microsoft.com> wrote in
message news:49**********************************@microsof t.com...
Consider the following code snippet:

using System;
enum E { A, }
struct T
{
public static explicit operator T(int i) { return new T(); }
public static void Foo()
{
E e = 0;
IntPtr ptr = (IntPtr)e; // case 1: compiles fine
T t = (T)e; // case 2: gives an error in correspondence with the
specs
}
}

Microsoft C# compiler gives an error in case 2 (which follows the specs).
In
case 1 the code compiles fine.
Does the compiler follow the specs in case 1? If so, where is the
description of that case in the specs?

I used C# compiler from VS 2003 and 2.0 November preview.

Thanks,
andy

Nov 17 '05 #2
Boxing can only occur with type "object" and nothing else.
"Sylvain Lafontaine" <sylvain aei ca (fill the blanks, no spam please)>
schrieb im Newsbeitrag news:e8**************@TK2MSFTNGP14.phx.gbl...
I don't know but maybe a boxing case here.

--
Sylvain Lafontaine, ing.
MVP - Technologies Virtual-PC
E-mail: http://cerbermail.com/?QugbLEWINF
"Andrey Simanovsky" <An**************@discussions.microsoft.com> wrote in
message news:49**********************************@microsof t.com...
Consider the following code snippet:

using System;
enum E { A, }
struct T
{
public static explicit operator T(int i) { return new T(); }
public static void Foo()
{
E e = 0;
IntPtr ptr = (IntPtr)e; // case 1: compiles fine
T t = (T)e; // case 2: gives an error in correspondence with the
specs
}
}

Microsoft C# compiler gives an error in case 2 (which follows the specs).
In
case 1 the code compiles fine.
Does the compiler follow the specs in case 1? If so, where is the
description of that case in the specs?

I used C# compiler from VS 2003 and 2.0 November preview.

Thanks,
andy


Nov 17 '05 #3
cody <de********@gmx.de> wrote:
Boxing can only occur with type "object" and nothing else.


Not true - it also occurs with interfaces. For instance:

int x = 5;
IComparable ic = x; // Boxing here

I don't think that's what's going on here, but I thought it was worth
being precise :)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #4
On Wed, 25 May 2005 09:41:06 -0700, Andrey Simanovsky wrote:
E e = 0;
IntPtr ptr = (IntPtr)e; // case 1: compiles fine Does the compiler follow the specs in case 1? If so, where is the
description of that case in the specs?


I looked in the spec and came to the conclusion that the compiler isn't
following the spec. The only way this could possibly work is by using the
explicit conversion operator.

public static explicit operator IntPtr(int value);

This however requires that we have two valid Standard Conversions. One from
IntPtr->IntPtr and one from E->int. The first is an identity conversion,
but the second seem to have neither an implicit or explicit standard
conversion as far as I can see.

--
Marcus Andrén
Nov 17 '05 #5
Thanks Marcus,

I also think that the specs do not reflect the compiler behavior. Just
wanted to make sure that I do not misread them.

"Marcus Andren" wrote:
On Wed, 25 May 2005 09:41:06 -0700, Andrey Simanovsky wrote:
E e = 0;
IntPtr ptr = (IntPtr)e; // case 1: compiles fine

Does the compiler follow the specs in case 1? If so, where is the
description of that case in the specs?


I looked in the spec and came to the conclusion that the compiler isn't
following the spec. The only way this could possibly work is by using the
explicit conversion operator.

public static explicit operator IntPtr(int value);

This however requires that we have two valid Standard Conversions. One from
IntPtr->IntPtr and one from E->int. The first is an identity conversion,
but the second seem to have neither an implicit or explicit standard
conversion as far as I can see.

--
Marcus Andrén

Nov 17 '05 #6

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

Similar topics

27
by: Mark A. Gibbs | last post by:
i have been toying with the idea of making my enums smarter - ie, more in line with the rest of the language. i haven't tested it yet, but what i came up with is a template like this: template...
3
by: MJB | last post by:
I'm getting an IStream back from function xmlHttp.responsestream. I would like to convert this to a System.IO.Stream in order to work with it in my application. Has anyone encountered this and...
2
by: SpotNet | last post by:
Hello CSharpies, Can Enums in C# be UInt32 Types, and not just Int32 Types. I have a lot of constant declarations that are UInt32 that I want to put in Enums to ease the use of Intellisence. I...
1
by: Vinoth Kumar | last post by:
Hi I am writing a wrapper class for a unmanaged c++ class, which has the method signature, Method(wchar_t** ppszValue) Anyone please tell me, how to convert the string _gc* _gc* to...
1
by: Mad Scientist Jr | last post by:
hi can someone possibly convert this to vb.net ? it is c# code that lets the pocket pc access the voice recorder... many thanks... source: wapboy...
5
by: Sakharam Phapale | last post by:
Hi All, I am using Microsoft Speech DLL "sapi.dll". I want to use following method of "SpeechLib.SpStream" object (following is a signature which I copied from Quick Info feature of .NET code...
8
by: Serge BRIC | last post by:
My application, written in .NET VB, tries to get a communication port handle from a TAPI object with this code: Dim vFileHandle As Byte() = appel.GetIDAsVariant("comm/datamodem") The...
3
by: MariuszC | last post by:
Hello, I have used some library where is defined enum like: typedef enum Enum1 { ENUM1_1, ENUM1_2, ENUM1_3, ENUM1_4, ENUM1_5 }; In my code I have defined: typedef enum Enum2 { ENUM2_1,...
9
by: =?iso-8859-1?B?S2VyZW0gR/xtcvxrY/w=?= | last post by:
Hi, i am a little stuck here, how do i convert this from C Structure to an structure in C# that can be marshalled: typedef struct _DEV_BROADCAST_DEVICEINTERFACE { DWORD dbcc_size; DWORD...
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...
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:
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.