Hi
VS 2005 beta 2 successfully compiles the following:
using System;
using System.Collecti ons.Generic;
using System.Text;
namespace ConsoleApplicat ion1 {
class Program {
enum A { a, b, c }
static void Main(string[] args) {
A? aaaa = null;
switch (aaaa) {
case null:
Console.WriteLi ne("null");
break;
default:
Console.WriteLi ne("def");
break;
}
}
}
}
Any comments on nullables in switch?
Thank you
Yuriy
Nov 17 '05
27 5639
How does it cover nullables? SomeEnum type is not base type for Nullable<SomeEn um>
and no impllicit conversion exists from Nullable<A> to A. Yuri,
Quoting the C# Specification, 3rd Edition (p. 228):
"The governing type of a switch statement is established by the switch expression. If the type of the switch expression is sbyte, byte, short, ushort, int, uint, long, ulong, char, string, or an enum-type, then that is the governing type of the switch statement. Otherwise, exactly one user-defined implicit
conversion operator (§13.4) shall exist from the type of the switch expression or a base type of this type to
one of the following possible governing types: sbyte, byte, short, ushort, int, uint, long, ulong,
char, string. If no such implicit conversion operator exists, or if more than one such implicit conversion
operator exists, a compile-time error occurs."
This covers the case of a nullable type as switch expression, right?
The standard is at www.ecma-international.org.
Privet - Octavio
"Yuriy Solodkyy" <y.************ @gmail.com> escribió en el mensaje news:7c******** *************** ***@msnews.micr osoft.com...
hi,
which types are allowed in switch? string and integral types? or any type, if there is only one way of implicit convertion to any of the types mentioned above.
Nullable<> is neither string nor integral type, and I cannot find anything where it is said that C# 2.0 enhances a list of types allowed in switch.
yuriy
Yuriy Solodkyy wrote:
Any comments on nullables in switch?
I'm sure it's me, but I don't understand the question. :-)
Oliver Sturm
In this case 42 is converted to int?, but it does not explain how switch
works. Specifications says that it must be either listed type or implicit
conversion to the listed type must exist. Yuriy Solodkyy wrote:
Nullable<Snum> is not enum itself. It is just value type, which has no implicit conversion to niether the enum or listed type. so, according to your explanation in cannot be used in switch, but it works. I wasn't saying that Nullable<Enum> is an Enum. I'm sure the conversion that's taking place is actually covered by paragraph 24.2 of the C# 2.0 specs - read it if you like, I guess the relevant point for your sample is the last one in 24.2.2:
If the nullable conversion is from S? to T, the conversion is evaluated as an unwrapping from S? to S followed by the underlying conversion from S to T.
If you decompile a program similar to your sample, you'll see that the compiler creates calls to the GetValueOrDefau lt method of the Nullable<T>, before the comparison itself takes place. This allows for constructs like
int? foo = 42; if (foo == 42) ... Or of course for the use of the Nullable<T> in a switch statement, such as we were discussing.
Oliver Sturm
Hi Octavio,
see comment inline
"Octavio Hernandez" <do****@danysof t.com> schrieb im Newsbeitrag
news:ue******** ******@TK2MSFTN GP14.phx.gbl... Yuri,
Quoting the C# Specification, 3rd Edition (p. 228):
"The governing type of a switch statement is established by the switch expression. If the type of the switch expression is sbyte, byte, short, ushort, int, uint, long, ulong, char, string, or an enum-type,
a nullable type is non if this types, right?
then that is the governing type of the switch statement. Otherwise, exactly one user-defined implicit
conversion operator (§13.4) shall exist from the type of the switch expression or a base type of this type to
one of the following possible governing types: sbyte, byte, short, ushort, int, uint, long, ulong,
char, string. If no such implicit conversion operator exists, or if more than one such implicit conversion
There is no user-defined conversion operator from a nullable type (if its
not defined by the user.), right?
operator exists, a compile-time error occurs."
This covers the case of a nullable type as switch expression, right?
No The standard is at www.ecma-international.org.
Privet - Octavio "Yuriy Solodkyy" <y.************ @gmail.com> escribió en el mensaje news:7c******** *************** ***@msnews.micr osoft.com... hi,
which types are allowed in switch? string and integral types? or any type, if there is only one way of implicit convertion to any of the types mentioned above.
Nullable<> is neither string nor integral type, and I cannot find anything where it is said that C# 2.0 enhances a list of types allowed in switch.
yuriy Yuriy Solodkyy wrote:
Any comments on nullables in switch?
I'm sure it's me, but I don't understand the question. :-)
Oliver Sturm
but with VS2005beta2 you can compile it and it works.. Hi Octavio, see comment inline "Octavio Hernandez" <do****@danysof t.com> schrieb im Newsbeitrag news:ue******** ******@TK2MSFTN GP14.phx.gbl...
Yuri,
Quoting the C# Specification, 3rd Edition (p. 228):
"The governing type of a switch statement is established by the switch expression. If the type of the switch expression is sbyte, byte, short, ushort, int, uint, long, ulong, char, string, or an enum-type, a nullable type is non if this types, right?
then that is the governing type of the switch statement. Otherwise, exactly one user-defined implicit
conversion operator (§13.4) shall exist from the type of the switch expression or a base type of this type to
one of the following possible governing types: sbyte, byte, short, ushort, int, uint, long, ulong,
char, string. If no such implicit conversion operator exists, or if more than one such implicit conversion There is no user-defined conversion operator from a nullable type (if its not defined by the user.), right?
operator exists, a compile-time error occurs."
This covers the case of a nullable type as switch expression, right? No
The standard is at www.ecma-international.org.
Privet - Octavio
"Yuriy Solodkyy" <y.************ @gmail.com> escribió en el mensaje news:7c******** *************** ***@msnews.micr osoft.com...
hi,
which types are allowed in switch? string and integral types? or any type, if there is only one way of implicit convertion to any of the types mentioned above.
Nullable<> is neither string nor integral type, and I cannot find anything where it is said that C# 2.0 enhances a list of types allowed in switch.
yuriy
Yuriy Solodkyy wrote:
> Any comments on nullables in switch? > I'm sure it's me, but I don't understand the question. :-)
Oliver Sturm
> If the nullable conversion is from S? to T, the conversion is evaluated as an unwrapping from S? to S followed by the underlying conversion from S to T.
The nullable conversion from S? to T is an Explicit conversion.
This allows for constructs like
int? foo = 42; if (foo == 42)
This is using the lifted == operator, which is covered in 14.2.7 of the ecma
spec.
Sorry, Yura!
Now I understand what you mean. Very interesting!
Regards - Octavio
"Yuriy Solodkyy" <y.************ @gmail.com> escribió en el mensaje
news:7c******** *************** ***@msnews.micr osoft.com... but with VS2005beta2 you can compile it and it works..
Hi Octavio, see comment inline "Octavio Hernandez" <do****@danysof t.com> schrieb im Newsbeitrag news:ue******** ******@TK2MSFTN GP14.phx.gbl...
Yuri,
Quoting the C# Specification, 3rd Edition (p. 228):
"The governing type of a switch statement is established by the switch expression. If the type of the switch expression is sbyte, byte, short, ushort, int, uint, long, ulong, char, string, or an enum-type, a nullable type is non if this types, right?
then that is the governing type of the switch statement. Otherwise, exactly one user-defined implicit
conversion operator (§13.4) shall exist from the type of the switch expression or a base type of this type to
one of the following possible governing types: sbyte, byte, short, ushort, int, uint, long, ulong,
char, string. If no such implicit conversion operator exists, or if more than one such implicit conversion There is no user-defined conversion operator from a nullable type (if its not defined by the user.), right?
operator exists, a compile-time error occurs."
This covers the case of a nullable type as switch expression, right? No
The standard is at www.ecma-international.org.
Privet - Octavio
"Yuriy Solodkyy" <y.************ @gmail.com> escribió en el mensaje news:7c******** *************** ***@msnews.micr osoft.com...
hi,
which types are allowed in switch? string and integral types? or any type, if there is only one way of implicit convertion to any of the types mentioned above.
Nullable<> is neither string nor integral type, and I cannot find anything where it is said that C# 2.0 enhances a list of types allowed in switch.
yuriy
> Yuriy Solodkyy wrote: > >> Any comments on nullables in switch? >> > I'm sure it's me, but I don't understand the question. :-) > > Oliver Sturm >
> but with VS2005beta2 you can compile it and it works..
I cannot find anyplace in the spec that would explain why it compiles. I
think this is an interesting question and I will take another look when I
have more time.
Note that the C# compiler also lets you switch on bool (which is a compiler
bug).
MarkT [developmentor] wrote: If the nullable conversion is from S? to T, the conversion is evaluated as an unwrapping from S? to S followed by the underlying conversion from S to T.
The nullable conversion from S? to T is an Explicit conversion.
I would think so, too, but that's nevertheless exactly what the generated
code shows.
Oliver Sturm
--
Expert programming and consulting services available
See http://www.sturmnet.org (try /blog as well)
> Any comments on nullables in switch?
I just spent an hour looking at every place either nullable or switch
appears in the spec, and I could not find anything that explains the
behavior. However, the spec is pretty tough reading so I could easily have
missed something. I tentatively vote "bug" on this one. You could submit it
as a bug in the Microsoft Product Feedback Center to get some official word
on it.
"MarkT [developmentor]" <Ma************ ****@discussion s.microsoft.com > wrote
in message news:C4******** *************** ***********@mic rosoft.com... Any comments on nullables in switch?
I just spent an hour looking at every place either nullable or switch appears in the spec, and I could not find anything that explains the behavior. However, the spec is pretty tough reading so I could easily have missed something. I tentatively vote "bug" on this one. You could submit it as a bug in the Microsoft Product Feedback Center to get some official word on it.
Note that only the latest v2.0 beta version (2.0.50727) includes the latest
Design Change Request made for VS2005 and the CLR, one of these DCR relate
to nullable types, maybe one could try this using this build.
Note that it's allways safe to file an issue to Product Feedback though.
Willy. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: John Wood |
last post by:
I was just looking at an article about using nullable value types. (value
types that can effectively have no value and not be set).
The syntax is to append a question-mark to the value type in the
declaration, eg:
int? age;
I don't like that much, I think it would be much more consistent to use a
new keyword, such as "nullable". But anyways...
|
by: ESPNSTI |
last post by:
Hi,
Please don't shoot me if the answer is simple, I'm new to .Net and C# :)
..
I'm attempting to convert a nullable type to it's "non-nullable" type in a
generic way (without knowing what specific type the nullable type is.)
The reason I'm trying this is because when I attempt to pass a nullable type
value to a SqlCommand parameter and then attempt to execute it I the
following error:
"No mapping exists from object type...
|
by: Joe Bloggs |
last post by:
Hi,
Can someone please kindly show me how to determine if a type (read value
type) is Nullable.
MSDN has this KB:
How to: Identify a Nullable Type (C# Programming Guide)
http://msdn2.microsoft.com/en-us/library/ms366789.aspx
however, using their code snippet, I couldn't get it to work:
|
by: GG |
last post by:
I am trying to add a nullable datetime column to a datatable fails. I am
getting exception
DataSet does not support System.Nullable<>.
None of these works
dtSearchFromData.Columns.Add( new DataColumn( "StartDate",
typeof( DateTime? ) ) );
dtSearchFromData.Columns.Add( new DataColumn( "EndDate",
typeof( System.Nullable<DateTime>) ) );
Any ideas?
|
by: Sam Kong |
last post by:
Hello,
I want to define a generic class which should accept only nullable
types or reference types.
What's the best way to costrain it?
---------
class MyClass<T>{
...
| |
by: John Grandy |
last post by:
Is it considered good practice to perform a switch operation on nullable
types ?
|
by: Dan Holmes |
last post by:
i need to get an object of type Type when i have a string. That is the
easy part (Type.GetType("int")). What if i want that type to be
nullable? This doesn't work Type.GetType("int?"). It returns null.
This does work but i don't know how to know all that information at runtime.
Type.GetType("System.Nullable`1]")
dan
|
by: Tony Johansson |
last post by:
Hello!
These two declarations(1 and 2) are the same I assume.
1. System.Nullable<intnullable;
2. System.Nullable<intnullable = new System.Nullable<int();
So because these 1 and 2 are the same is no point to use the longer
declaration as 2 it good enough
to use decaration 1.
|
by: Tony Johansson |
last post by:
Hello!
I'm reading in a book called Visual C# 2005. In the chapter about Generics
there ia a section about Nullable types.
Here is the text that isn't complete true I think. It says:
"You can also look at the value of a reference type using the Value
property. If HasValue is true, then
you are guarantee a non-null value for Value, but if HasValue is false,
that is, null has been assigned to the variable, then accessing Value will
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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 then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |