473,785 Members | 2,746 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

switch and nullable type in C# 2.0

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 #1
27 5638
Yuriy Solodkyy wrote:
Any comments on nullables in switch?


I'm sure it's me, but I don't understand the question. :-)

Oliver Sturm
--
Expert programming and consulting services available
See http://www.sturmnet.org (try /blog as well)

Nov 17 '05 #2
Hi,

Don't see any problems with that...

Regards - Octavio

"Yuriy Solodkyy" <y.************ @gmail.com> escribió en el mensaje
news:7c******** *************** ***@msnews.micr osoft.com...
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 #3
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

Nov 17 '05 #4
Yuriy Solodkyy wrote:
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.


Here's what the specs say (C# 1.2):

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 (§?6.4) must exist from the type of the
switch expression to one of the following possible governing types: sbyte,
byte, short, ushort, int, uint, long, ulong, char, string. If no such
implicit conversion exists, or if more than one such implicit conversion
exists, a compile-time error occurs.

The constant expression of each case label must denote a value of a type
that is implicitly convertible (§?6.1) to the governing type of the switch
statement. A compile-time error occurs if two or more case labels in the
same switch statement specify the same constant value.

Oliver Sturm
--
Expert programming and consulting services available
See http://www.sturmnet.org (try /blog as well)

Nov 17 '05 #5
What is Nullable<A> in this list? Do you know any implicit convertions from
A? to any of the types listed in spec?

yuriy

Yuriy Solodkyy wrote:
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.

Here's what the specs say (C# 1.2):

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 (§?6.4) must exist from
the type of the switch expression to one of the following possible
governing types: sbyte, byte, short, ushort, int, uint, long, ulong,
char, string. If no such implicit conversion exists, or if more than
one such implicit conversion exists, a compile-time error occurs.

The constant expression of each case label must denote a value of a
type that is implicitly convertible (§?6.1) to the governing type of
the switch statement. A compile-time error occurs if two or more case
labels in the same switch statement specify the same constant value.

Oliver Sturm

Nov 17 '05 #6
Yuriy Solodkyy wrote:
What is Nullable<A> in this list? Do you know any implicit convertions
from A? to any of the types listed in spec?


A Nullable<T> can be used in a switch statement if T itself has an
implicit conversion to one of the listed types, or is a listed type. In
your case, that's the Enum. If you try your sample with a Nullable<T>
where T is an arbitrary class, you'll see that the compiler complains
about it, even if you're still only checking for the null case.

I wonder why you're wondering about this so much... to my mind, this
construct behaves just like one would think it should, doesn't it? IOW, if
a type T can be used in a switch, then T? can be used as well, and can
additionally be checked for the null case. Makes a lot of sense to me.
Oliver Sturm
--
Expert programming and consulting services available
See http://www.sturmnet.org (try /blog as well)

Nov 17 '05 #7
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.
Yuriy Solodkyy wrote:
What is Nullable<A> in this list? Do you know any implicit
convertions from A? to any of the types listed in spec?

A Nullable<T> can be used in a switch statement if T itself has an
implicit conversion to one of the listed types, or is a listed type.
In your case, that's the Enum. If you try your sample with a
Nullable<T> where T is an arbitrary class, you'll see that the
compiler complains about it, even if you're still only checking for
the null case.

I wonder why you're wondering about this so much... to my mind, this
construct behaves just like one would think it should, doesn't it?
IOW, if a type T can be used in a switch, then T? can be used as well,
and can additionally be checked for the null case. Makes a lot of
sense to me.

Oliver Sturm

Nov 17 '05 #8
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


Nov 17 '05 #9
Yuriy Solodkyy wrote:
Nullable<Snu m> 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
--
Expert programming and consulting services available
See http://www.sturmnet.org (try /blog as well)

Nov 17 '05 #10

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

Similar topics

10
1832
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...
4
5972
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...
1
5681
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:
5
51677
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?
8
10704
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>{ ...
5
6528
by: John Grandy | last post by:
Is it considered good practice to perform a switch operation on nullable types ?
1
2503
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
2
180
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.
6
1471
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
0
9643
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, 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...
0
10319
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10147
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 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...
1
10087
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,...
0
9947
tracyyun
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...
1
7496
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5380
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...
2
3645
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2877
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.