473,387 Members | 1,549 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

array conversion

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).
Nov 16 '05 #1
2 1638
Sankar Nemani <sn*****@nospamlumedx.com> 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 - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2
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
Nov 16 '05 #3

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

Similar topics

58
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of...
8
by: chessc4c6 | last post by:
The program below creates a char pointer call charPtr...... i then declare an char array string "Good Luck" When i assign charPtr = string, I expect an error. However, It actually runs and...
11
by: x-pander | last post by:
given the code: <file: c.c> typedef int quad_t; void w0(int *r, const quad_t *p) { *r = (*p); }
204
by: Alexei A. Frounze | last post by:
Hi all, I have a question regarding the gcc behavior (gcc version 3.3.4). On the following test program it emits a warning: #include <stdio.h> int aInt2 = {0,1,2,4,9,16}; int aInt3 =...
9
by: Codemonkey | last post by:
Hi, Sorry for a stupid question, but is it possible to do a narrowing conversion with an object array with Option Strict On in VB? E.g: ------------------ Dim aBase as Base() = {New...
20
by: subramanian | last post by:
Hello I have a doubt in the following piece of code: int a; printf("a=%p\n", a); printf("&a=%p\n", &a); these printf statements print the same value for both 'a' and '&a". I tried in...
0
by: k1ckthem1dget | last post by:
I need to display the unsorted list of names and display the sorted list of names. My program is getting a bunch of errors though, and i dont know why. I am getting the following errors. 28:...
2
by: k1ckthem1dget | last post by:
I need to display the unsorted list of names and display the sorted list of names. My program is getting a bunch of errors though, and i dont know why. I am getting the following errors. 28:...
6
by: Lighter | last post by:
How to read "The lvalue-to-rvalue, array-to-pointer, and function-to- pointer standard conversionsare not applied to the left expressions"? In 5.18 Comma operator of the C++ standard, there is a...
152
by: vippstar | last post by:
The subject might be misleading. Regardless, is this code valid: #include <stdio.h> void f(double *p, size_t size) { while(size--) printf("%f\n", *p++); } int main(void) { double array = { {...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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,...
0
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...

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.