What is "Friend entries"?
Casting null to nullable
Hello Guest
  
  • Login
• Register…
• Start blog
  • Who, Where, When
• What can I do?
• What to Read?
  • Polls
• Avatars
• Interests
  • Cities and Countries
• Random blog
• Users search
  • Search
• Games
• Tests
• QAIX
  • Сообщества
• Talxy Chat
• Horoscope
• Online
 
Зарегистрируйся!

QAIX > .Net Development > Casting null to nullable 16 November 2007 11:12:22

  Recent blog posts: 
  They have birthday today: 
  Forums:   
  Discuss: 
  Recent forum topics: 
  Recent forum comments:
  Moderators:

Casting null to nullable

Brady Kelly 16 November 2007 11:12:22
 I find this requirement quite unintuitive and clumsy. Is there anything to
be said in its defence? I thought nullable types were supposed to save us
this kind of friction.


===================­================
This list is hosted by DevelopMentor http://www.develop.­com

View archives and manage your subscription(s) at http://discuss.deve­lop.com

Add comment
Jon Skeet 15 November 2007 13:27:42 permanent link ]
 
I find this requirement quite unintuitive and clumsy. Is there anything
to be said in its defence? I thought nullable types were supposed
to save us this kind of friction.

Could you say exactly what requirement you mean?

You can do:

int? x = null;

with no explicit cast, for instance.

Jon

===================­================
This list is hosted by DevelopMentor® http://www.develop.­com

View archives and manage your subscription(s) at http://discuss.deve­lop.com

Add comment
Rich Armstrong 15 November 2007 18:04:53 permanent link ]
 I'm aware of one situation that requires an explicit cast:

// This produces an error "Type of conditional expression
// cannot be determined because there is no implicit conversion
// between 'int' and '<null>'"
int? nullableInt = someCondition ? 0 : null;

// This compiles without error
int? nullableInt = someCondition ? 0 : (int?)null;

I'll reserve judgment on how intuitive the requirement may or not be.



Date: Thu, 15 Nov 2007 10:27:42 +0000> From: skeet@POBOX.COM> Subject: Re: [DOTNET-CLR] Casting null to nullable> To: DOTNET-CLR@DISCUSS.­DEVELOP.COM> > > I find this requirement quite unintuitive and clumsy. Is there anything> > to be said in its defence? I thought nullable types were supposed> > to save us this kind of friction.> > Could you say exactly what requirement you mean?> > You can do:> > int? x = null;> > with no explicit cast, for instance.> > Jon> > ===================­================> This list is hosted by DevelopMentor® http://www.develop.­com> > View archives and manage your subscription(s) at http://discuss.deve­lop.com
===================­================
This list is hosted by DevelopMentor® http://www.develop.­com

View archives and manage your subscription(s) at http://discuss.deve­lop.com

Add comment
Curt Hagenlocher 15 November 2007 18:25:45 permanent link ]
 On 11/15/07, Rich Armstrong <richa@msn.com> wrote:
I'm aware of one situation that requires an explicit cast:
// This produces an error "Type of conditional expression
// cannot be determined because there is no implicit conversion
// between 'int' and '<null>'"
int? nullableInt = someCondition ? 0 : null;
// This compiles without error
int? nullableInt = someCondition ? 0 : (int?)null;


I'll bet you can also say
int? nullableInt = someCondition ? (int?)0 : null

The compiler has to be able to find a common type between "0" and "null" in
order to generate code, and apparently it doesn't in this case. The fact
that the variable declaration is "int?" is a bit of a red herring as the
type of the lhs of the expression is not taken into account when evaluating
the rhs.

--
Curt Hagenlocher
curt@hagenlocher.or­g

===================­================
This list is hosted by DevelopMentor® http://www.develop.­com

View archives and manage your subscription(s) at http://discuss.deve­lop.com

Add comment
Brady Kelly 16 November 2007 11:12:22 permanent link ]
 I came to the same conclusion this morning, while taking my walk, and have
just confirmed this.

-----Original Message-----
From: Discussion of development on the .NET platform using any managed
language [mailto:D­OTNET-CLR@DISCUSS.DEVELOP.COM] On Behalf Of Curt
Hagenlocher
Sent: 15 November 2007 05:26 PM
To: DOTNET-CLR@DISCUSS.­DEVELOP.COM
Subject: Re: [DOTNET-CLR] Casting null to nullable
On 11/15/07, Rich Armstrong <richa@msn.com> wrote:
I'm aware of one situation that requires an explicit cast:
// This produces an error "Type of conditional expression
// cannot be determined because there is no implicit conversion
// between 'int' and '<null>'"
int? nullableInt = someCondition ? 0 : null;
// This compiles without error
int? nullableInt = someCondition ? 0 : (int?)null;
I'll bet you can also say
int? nullableInt = someCondition ? (int?)0 : null
The compiler has to be able to find a common type between "0" and
"null" in
order to generate code, and apparently it doesn't in this case. The
fact
that the variable declaration is "int?" is a bit of a red herring as
the
type of the lhs of the expression is not taken into account when
evaluating
the rhs.
--
Curt Hagenlocher
curt@hagenlocher.or­g
===================­================
This list is hosted by DevelopMentorR http://www.develop.­com
View archives and manage your subscription(s) at

===================­================
This list is hosted by DevelopMentor http://www.develop.­com

View archives and manage your subscription(s) at http://discuss.deve­lop.com

Add comment
 

Add new comment

As:
Login:  Password:  
 
 
  
 
Пожалуйста, относитесь к собеседникам уважительно, не используйте нецензурные слова, не злоупотребляйте заглавными буквами, не публикуйте рекламу и объявления о купле/продаже, а также материалы нарушающие сетевой этикет или УК РФ.


QAIX > .Net Development > Casting null to nullable 16 November 2007 11:12:22

see also:
[patch 4/9] Fix use of CAST5 in…
Values list-of-targetlists patch for…
Patch for - Change FETCH/MOVE to use…
пройди тесты:
Do you really know yourself?
see also:
MY SISTER DREAMT SO MUCH ABOUT ME THOSE…
MY SISTER AND ME, WOULD LAUGH TOGETHER…
MY SISTER GOT A LITTLE ANGRY WITH ME…

  Copyright © 2001—2008 QAIX
Idea: Miсhael Monashev
Помощь и задать вопросы можно в сообществе support.qaix.com.
Сообщения об ошибках оставляем в сообществе bugs.qaix.com.
Предложения и комментарии пишем в сообществе suggest.qaix.com.
Информация для родителей.
Write us at:
If you would like to report an abuse of our service, such as a spam message, please .