PC Review


Reply
Thread Tools Rate Thread

array conversion

 
 
Sankar Nemani
Guest
Posts: n/a
 
      9th Jan 2005
Why does the following code throw an exception?

object[] o = new object[2]{"sankar", "sankar2"};
string[] s;
s =(string[])o;


The C# language spec says something different on MSDN:
For any two reference-types A and B, if an implicit reference conversion
(Section 6.1.4) or explicit reference conversion (Section 6.2.3) exists from
A to B, then the same reference conversion also exists from the array type
A[R] to the array type B[R], where R is any given rank-specifier (but the
same for both array types).


 
Reply With Quote
 
 
 
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      9th Jan 2005
Sankar Nemani <(E-Mail Removed)> wrote:
> Why does the following code throw an exception?
>
> object[] o = new object[2]{"sankar", "sankar2"};
> string[] s;
> s =(string[])o;


Because you haven't created a string array. You've created an object
array and populated it with strings. There's a big difference.

> The C# language spec says something different on MSDN:
> For any two reference-types A and B, if an implicit reference conversion
> (Section 6.1.4) or explicit reference conversion (Section 6.2.3) exists from
> A to B, then the same reference conversion also exists from the array type
> A[R] to the array type B[R], where R is any given rank-specifier (but the
> same for both array types).


This is interesting - it's okay at compile time because of that, but
it's not okay at runtime. What you've done is the equivalent of:

object o = new object();
string s = (string) o;

The explicit conversion is okay at compile time because of the first
listed explicit reference conversion. However, it's not valid at
runtime. The spec doesn't make this clear at all, as far as I can see


--
Jon Skeet - <(E-Mail Removed)>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
Cor Ligthert
Guest
Posts: n/a
 
      9th Jan 2005
Sankar,

I answered you in the VBlanguage group, where you left the documentation by
the way, in C# my answer would have been that it should be in my opinion.

>object[] o = new object[2]{"sankar", "sankar2"};
>string[] s;
>s =(string[])o;


object[] o = new string[2]{"sankar", "sankar2"};
string[] s;
s =(string[])o;

And than some text however that you can read there.

Cor


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Implicit conversion of an array =?Utf-8?B?RGF2ZQ==?= Microsoft Dot NET 1 8th Feb 2006 02:22 AM
Array conversion question Pavils Jurjans Microsoft C# .NET 6 11th Oct 2005 02:54 PM
string array conversion Jimmy Microsoft C# .NET 2 21st May 2005 03:20 AM
Value Type Conversion to Array David Kyle Microsoft ASP .NET 2 19th Nov 2004 10:04 PM
Re: Byte Array conversion to hex Mattias Sjögren Microsoft C# .NET 2 3rd Sep 2004 01:51 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:32 PM.