Ureader.com  
Microsoft software help and Community
   home   |   control panel login   |   archive   |  
 
DotNet
acad.assignment.mngr
academic
adonet
aspnet
aspnet.announcements
aspnet.build.controls
aspnet.caching
aspnet.datagridcontrol
aspnet.mobile
aspnet.security
aspnet.webcontrols
aspnet.webservices
clr
compactframework
component_services
datatools
distributed_apps
drawing
faqs
framework
framework.wmi
general
internationalization
interop
languages.csharp
languages.jscript
languages.vb
languages.vb.controls
languages.vb.data
languages.vb.upgrade
languages.vc
languages.vc.libraries
myservices
odbcnet
performance
remoting
scripting
sdk
security
setup
vjsharp
vsa
webservi.enhancements
webservices
windowsforms
windowsforms.controls
winforms.databinding
winforms.designtime
xml
  
 
date: Thu, 14 Aug 2008 09:04:37 -0700 (PDT),    group: microsoft.public.dotnet.framework        back       


.NET 3.5 SP1 causes exception when casting arrays (using .Cast<>)   
I just installed Service Pack 1 for the .NET Framework 3.5. The
following (greatly simplified) code now throws an exception on the
Cast<> line:

    class Program
    {
        static void Main(string[] args)
        {
            int[] ints = { 1, 2, 3, 4 };

            string[] strings = ints.Cast<string>().ToArray(); //
InvalidCastException
                                            // (Unable to cast object
of type 'System.Int32' to type 'System.String'.)
        }
    }


Has anyone else seen this; is this a bug in SP1?
date: Thu, 14 Aug 2008 09:04:37 -0700 (PDT)   author:   Richard Everett

Re: .NET 3.5 SP1 causes exception when casting arrays (using .Cast<>)   
Richard Everett wrote:
> I just installed Service Pack 1 for the .NET Framework 3.5. The
> following (greatly simplified) code now throws an exception on the
> Cast<> line:
>
>     class Program
>     {
>         static void Main(string[] args)
>         {
>             int[] ints = { 1, 2, 3, 4 };
>
>             string[] strings = ints.Cast<string>().ToArray(); //
> InvalidCastException
>                                             // (Unable to cast object
> of type 'System.Int32' to type 'System.String'.)
>         }
>     }
>
>
> Has anyone else seen this; is this a bug in SP1?

Yes, and this is by design. Here is the explanation:

http://blogs.msdn.com/ed_maurer/archive/2008/02/16/breaking-change-in-linq-queries-using-explicitly-typed-range-variables.aspx

Long story short, you were never supposed to do anything but object
upcasts and downcasts using Cast(). When you need to actually convert
values from one type to another, you should use Select(), and spell
out the conversion explicitly; e.g.:

  ints.Select(x => x.ToString())
date: Thu, 14 Aug 2008 09:51:39 -0700 (PDT)   author:   Pavel Minaev

Re: .NET 3.5 SP1 causes exception when casting arrays (using .Cast<>)   
Richard Everett  wrote:
> I just installed Service Pack 1 for the .NET Framework 3.5. The
> following (greatly simplified) code now throws an exception on the
> Cast<> line:
> 
>     class Program
>     {
>         static void Main(string[] args)
>         {
>             int[] ints = { 1, 2, 3, 4 };
> 
>             string[] strings = ints.Cast<string>().ToArray(); //
> InvalidCastException
>                                             // (Unable to cast object
> of type 'System.Int32' to type 'System.String'.)
>         }
>     }
> 
> 
> Has anyone else seen this; is this a bug in SP1?

Did the above really work before SP1? That would surprise me greatly. 
What *did* work, but now doesn't, is something like:

new int[]{1, 2, 3}.Cast<double>().ToArray()

In other words where there *is* an available conversion (int to double 
in this case). Pavel has given a link to an explanation.

But as there's no direct conversion available between ints and strings, 
I'd be very surprised to see the above work pre-SP1.

-- 
Jon Skeet - 
Web site: http://www.pobox.com/~skeet   
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
date: Thu, 14 Aug 2008 20:08:16 +0100   author:   Jon Skeet [C# MVP]

Re: .NET 3.5 SP1 causes exception when casting arrays (using .Cast<>)   
Use anonymous delgates

List<int> liInts;
List<string> lsStrings;

liInts = new List<int>(ints);

lsStrings = liInts.ConvertAll<string>(delegate(int aiValue)
                                                       {
                                                            return 
Convert.ToString(aiValue);
                                                       });



liContractItemKeys = lsContractItemKeys.ConvertAll<int>(delegate(string 
asValue) { return (Convert.ToInt32(asValue)); });


"Richard Everett"  wrote in message 
news:206a22ac-3d95-4781-b3e7-7b12c34d5074@r66g2000hsg.googlegroups.com...
>I just installed Service Pack 1 for the .NET Framework 3.5. The
> following (greatly simplified) code now throws an exception on the
> Cast<> line:
>
>    class Program
>    {
>        static void Main(string[] args)
>        {
>            int[] ints = { 1, 2, 3, 4 };
>
>            string[] strings = ints.Cast<string>().ToArray(); //
> InvalidCastException
>                                            // (Unable to cast object
> of type 'System.Int32' to type 'System.String'.)
>        }
>    }
>
>
> Has anyone else seen this; is this a bug in SP1?
date: Thu, 14 Aug 2008 15:26:21 -0500   author:   Techno_Dex

Re: .NET 3.5 SP1 causes exception when casting arrays (using .Cast<>)   
Techno_Dex  wrote:
> Use anonymous delgates
> 
> List<int> liInts;
> List<string> lsStrings;
> 
> liInts = new List<int>(ints);
> 
> lsStrings = liInts.ConvertAll<string>(delegate(int aiValue)
>                                                        {
>                                                             return 
> Convert.ToString(aiValue);
>                                                        });
> 
> liContractItemKeys = lsContractItemKeys.ConvertAll<int>(delegate(string 
> asValue) { return (Convert.ToInt32(asValue)); });

If you're using .NET 3.5 to start with, you've got C# 3 available, so 
there's a much nicer way open:

ints.Select(i => i.ToString())

That way it's still lazy too :)

-- 
Jon Skeet - 
Web site: http://www.pobox.com/~skeet   
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
date: Thu, 14 Aug 2008 22:57:57 +0100   author:   Jon Skeet [C# MVP]

Re: .NET 3.5 SP1 causes exception when casting arrays (using .Cast<>)   
On Aug 14, 11:08 pm, Jon Skeet [C# MVP]  wrote:
> Richard Everett  wrote:
> >             int[] ints = { 1, 2, 3, 4 };
>
> >             string[] strings = ints.Cast<string>().ToArray(); //
>
> Did the above really work before SP1? That would surprise me greatly.

Actually, yes, it did work - it was a consequence of them using
Convert.ChangeType() (and thus IConvertible). As I understand,
previously, Cast() first tried a straightforward cast (via as), and
falled back to Convert if that failed.
date: Thu, 14 Aug 2008 22:54:06 -0700 (PDT)   author:   Pavel Minaev

Re: .NET 3.5 SP1 causes exception when casting arrays (using .Cast<>)   
On Aug 15, 6:54 am, Pavel Minaev  wrote:
> > Richard Everett  wrote:
> > >             int[] ints = { 1, 2, 3, 4 };
>
> > >             string[] strings = ints.Cast<string>().ToArray(); //
>
> > Did the above really work before SP1? That would surprise me greatly.
>
> Actually, yes, it did work - it was a consequence of them using
> Convert.ChangeType() (and thus IConvertible). As I understand,
> previously, Cast() first tried a straightforward cast (via as), and
> falled back to Convert if that failed.

Yes, I've just tried it on a pre-SP1 machine - apologies for adding to
the confusion :(

Jon
date: Fri, 15 Aug 2008 00:49:32 -0700 (PDT)   author:   Jon Skeet [C# MVP]

Re: .NET 3.5 SP1 causes exception when casting arrays (using .Cast<>)   
Thanks to all for the comprehensive responses!
date: Wed, 20 Aug 2008 03:28:18 -0700 (PDT)   author:   Richard Everett

Google
 
Web ureader.com


    COPYRIGHT 2007, YARDI TECHNOLOGY LIMITED, ALL RIGHT RESERVE  |   contact us