6 replies

Last post May 16, 2012 02:22 AM by amigo 1

• amigo 1

Member

55 Points

224 Posts

Casting an array

May 14, 2012 03:06 AM|amigo 1|LINK

let us create an array of type int like this:

Array intArray1 = Array.CreateInstance(typeof(int), 5);
for (int i = 0; i < 5; i++)
{
intArray1.SetValue(33, i);
}
for (int i = 0; i < 5; i++)
{
Console.WriteLine(intArray1.GetValue(i));
}

To cast the created array to an array declared as int[] we do as:

int[] intArray2 = (int[])intArray1;

my questions are:
q1)  Why do we need to cast it into an array already declared as int[]?
q2) Why does the casting looks like the above shown here?
Normally casting looks like the following:-(an e.g.)

double y;
// casting double into int
int x = int(y);

• tusharrs

Participant

1953 Points

646 Posts

Re: Casting an array

Why do we need to cast it into an array already declared as int[]?

Ans: As you have declared intArray1 as Array (Array intArray1 ), it is only an Array which consists of int data

so you have to use this (int[] intArray2 = (int[])intArray1;) to cast.

If you have declared it as

int[] intArray1;

intArray1 = new int[5];

then you can assign it directly as

int[] intArray2 = intArray1;

Why does the casting looks like the above shown here?

Ans: the type in which we have to convert is in parenthesis

so it should be,

double y = 10;

// casting double into int

int x = (int)y;

Regards

( )

View my Blog
• Rimbik

Member

741 Points

364 Posts

Re: Casting an array

I would like to say 'Avoid using Array class' its older style untill and unless you are bound to use it.

I never used Array in my entire life in .net since 2002 (anyway that noting to do with your requirement)

What I am trying to say is do not use anything that need typecase to use. Its  Not a good approach.

Use something like:

List<int> myList = new List<int>();

Budhi Hin Tanu Janike, Sumirau Pavan Kumar
Bal budhi Vidya dehu mohe,Harahu Kalesa Vikar
[My Site: WCF, My Site
and Silverlight site @ Silverlight version].
• amigo 1

Member

55 Points

224 Posts

Re: Casting an array

May 15, 2012 02:35 AM|amigo 1|LINK

with due regards to Rimbak and Tusharrs

I did not find the answers intuitive for let us see the case

"Ans: As you have declared intArray1 as Array (Array intArray1 ), it is only an Array which consists of int data

so you have to use this (int[] intArray2 = (int[])intArray1;) to cast.

If you have declared it as

int[] intArray1;

intArray1 = new int[5];

then you can assign it directly as

int[] intArray2 = intArray1;
"

Actually when ever we declare an array with the bracket syntax  behind the scenes
the Array class comes into play.The syntax of MY CODE is followed by the compiler behind the scenes
& the type is already sent with the "typeof" keyword.SO why such a casting ? Also in C# by
whatever  way an array is declared it is stored in a managed heap.

• tusharrs

Participant

1953 Points

646 Posts

Re: Casting an array

Array intArray1 = Array.CreateInstance(typeof(int), 5);

in this equation

Array intArray1 is the declaration

and Array.CreateInstance(typeof(int),5); is the assignment

so while declaring there is not specified which type of array it is

Regards

( )

View my Blog
• Paul Linton

Star

9555 Points

2784 Posts

Re: Casting an array

May 15, 2012 07:03 AM|Paul Linton|LINK

Q1) I can't quite tell if you understand the answers you have been given or not.  But I will try another answer.  Think of Array as a Base class and int[], string[], double[] and so on as derived classes.  Similar to having a base class of Animal and derived classes of Monkey, Elephant and so on.

Your declaration tells the compiler, in the first line of code, that the type of intArray1 is Array.  This is a rather strange thing to do because you actually know that the most specific type is int[].  Nevertheless you have demanded that the compiler use the variable intArray1 as if it was just of type Array.

Later in the code you decide that because you (the programmer) actually know that this thing (intArray1) that you formerly told the compiler was of type Array is actually of type int[] you want the compiler to also know.  You tell the compiler this by (int[])intArray1.

Q2)  The casting looks as shown because your alternative ( int(y) ) is not c#.  It may be some other language but it is not c#.  It will not compile.

Got a c# problem? Try .NET Book Zero from Charles Petzold, it's a free pdf.
• amigo 1

Member

55 Points

224 Posts

Re: Casting an array

May 16, 2012 02:22 AM|amigo 1|LINK

This is really enrichment

thanks all of you