Are these tree statement identical

  • Thread starter Thread starter Tony Johansson
  • Start date Start date
T

Tony Johansson

Hi!

I'm 99% sure that these three statement do the same thing but I just
want to get it confirmed that's why I ask ?

Decimal[] decimal1 = new decimal[5];
Array decimal2 = Array.CreateInstance(typeof(Decimal),5);
Array decimal3 = Array.CreateInstance(Type.GetType("Decimal"),5);

//Tony
 
Tony said:
Hi!

I'm 99% sure that these three statement do the same thing but I just
want to get it confirmed that's why I ask ?

Decimal[] decimal1 = new decimal[5];
Array decimal2 = Array.CreateInstance(typeof(Decimal),5);
Array decimal3 = Array.CreateInstance(Type.GetType("Decimal"),5);

The end result will be the same, but they don't accomplish it in the
same way.

You can use tools like ILDASM.EXE or Reflector to see exactly what the
differences are in compiler output, and of course you can always measure
the performance to see the difference there.

Pete
 
Tony Johansson said:
Hi!

I'm 99% sure that these three statement do the same thing but I just
want to get it confirmed that's why I ask ?

Decimal[] decimal1 = new decimal[5];
Array decimal2 = Array.CreateInstance(typeof(Decimal),5);
Array decimal3 = Array.CreateInstance(Type.GetType("Decimal"),5);

//Tony


.

I would say technically no. To access the third element of decimal2 or
decimal3, you must use this construct:

((IList) decimal2) [2] = 5M;

but with decimal1, this is fine:

decimal1 [2] = 5M;

Mike
 
Family said:
I would say technically no. To access the third element of decimal2 or
decimal3, you must use this construct:

((IList) decimal2) [2] = 5M;

Actually, this works fine:

Decimal[] decimal2 = (Decimal[])Array.CreateInstance(typeof(Decimal), 5);

decimal2[2] = 5M;

As does this:

((Decimal[])decimal2)[2] = 5M;

In other words, the actual objects created in each line of code have
exactly the same type and array length. They are for all intents and
purposes the same.

It's true that the variable declaration affects how you can access the
elements, but that's true independent of how the object is created, and
probably was not what Tony was asking about.

Pete
 
Family Tree Mike said:
Tony Johansson said:
Hi!

I'm 99% sure that these three statement do the same thing but I just
want to get it confirmed that's why I ask ?

Decimal[] decimal1 = new decimal[5];
Array decimal2 = Array.CreateInstance(typeof(Decimal),5);
Array decimal3 = Array.CreateInstance(Type.GetType("Decimal"),5);

//Tony


.

I would say technically no. To access the third element of decimal2 or
decimal3, you must use this construct:

((IList) decimal2) [2] = 5M;

but with decimal1, this is fine:

decimal1 [2] = 5M;

Mike

Hi!

This statement get runtime error
((IList) decimal2) [2] = 5M
but if I add the generic type It works
((IList<Decimal>) decimal2) [2] = 5M

As a summary #1 is very simple compare to #2 and #3
One more thing #3 can't just have the string "Decimal" it must be
"System.Decimal"

//Tony
 
Peter Duniho said:
Family said:
I would say technically no. To access the third element of decimal2 or
decimal3, you must use this construct:

((IList) decimal2) [2] = 5M;

Actually, this works fine:

Decimal[] decimal2 = (Decimal[])Array.CreateInstance(typeof(Decimal), 5);

decimal2[2] = 5M;

As does this:

((Decimal[])decimal2)[2] = 5M;

In other words, the actual objects created in each line of code have
exactly the same type and array length. They are for all intents and
purposes the same.

It's true that the variable declaration affects how you can access the
elements, but that's true independent of how the object is created, and
probably was not what Tony was asking about.

Pete
.

Thanks, you clarified what I was trying to describe. decimal2 and decimal3
are not indexable as declared. This is why I could never make a living as a
teacher!

Mike
 
Hi!

Having these as before
Decimal[] decimal1 = new decimal[5];
Array decimal2 = Array.CreateInstance(typeof(Decimal), 5);
Array decimal3 = Array.CreateInstance(Type.GetType("System.Decimal"), 5);

The type for each one is the same and it is System.Decimal[]
Console.WriteLine(decimal1.GetType()); //System.Decimal[]
Console.WriteLine(decimal2.GetType()); //System.Decimal[]
Console.WriteLine(decimal3.GetType()); //System.Decimal[]

Just wonder when the type is the same for all three what is the reason that
you must use these kind
of statement for decimal2 and decimal3
Decimal[] decimal2 = (Decimal[])Array.CreateInstance(typeof(Decimal), 5);
decimal temp = ((Decimal[])decimal2)[2];
((Decimal[])decimal2)[2] = 5M;

I mean when the type is the same I had imagine that you would be able to
access the decimal2 and decimal3 in exctly the same way as you do with
decimal1.

//Tony
 
Tony said:
[...]
Just wonder when the type is the same for all three what is the reason that
you must use these kind
of statement for decimal2 and decimal3
Decimal[] decimal2 = (Decimal[])Array.CreateInstance(typeof(Decimal), 5);
decimal temp = ((Decimal[])decimal2)[2];
((Decimal[])decimal2)[2] = 5M;

I mean when the type is the same I had imagine that you would be able to
access the decimal2 and decimal3 in exctly the same way as you do with
decimal1.

Your original code example declared "decimal2" as "Array", not
"Decimal[]". As such, if you want to access it as a strongly typed
"Decimal[]", it needs casting.

In the event that you declare the variable as the actual type (as shown
above), then of course you do not need additional casting to use the
variable that way (but you do need it in the initialization of the
variable, as shown).

Pete
 
Peter Duniho said:
Tony said:
[...]
Just wonder when the type is the same for all three what is the reason
that you must use these kind
of statement for decimal2 and decimal3
Decimal[] decimal2 = (Decimal[])Array.CreateInstance(typeof(Decimal), 5);
decimal temp = ((Decimal[])decimal2)[2];
((Decimal[])decimal2)[2] = 5M;

I mean when the type is the same I had imagine that you would be able to
access the decimal2 and decimal3 in exctly the same way as you do with
decimal1.

Your original code example declared "decimal2" as "Array", not
"Decimal[]". As such, if you want to access it as a strongly typed
"Decimal[]", it needs casting.

In the event that you declare the variable as the actual type (as shown
above), then of course you do not need additional casting to use the
variable that way (but you do need it in the initialization of the
variable, as shown).

Pete

Hi

I mean when I have this first statement #1 below I have as I believe
specified that I want 5 array elements of base type decimal just as I do
here decimal[] example = new decimal[5];
#1 Array myArray = Array.CreateInstance(typeof(Decimal), 5);

#2 Decimal[] myDecimal = (Decimal[])Array.CreateInstance(typeof(Decimal),
5);

So what I in some way don't fully understand is why I have to cast to an
array of decimal when I create an array of decimal like I do in #1

So is it correct to see it in this way because of having to use explicit
cast it is similar to when a method return an object which require explicit
cast to the wanted type.


//Tony
 
Tony said:
I mean when I have this first statement #1 below I have as I believe
specified that I want 5 array elements of base type decimal just as I do
here decimal[] example = new decimal[5];
#1 Array myArray = Array.CreateInstance(typeof(Decimal), 5);

#2 Decimal[] myDecimal = (Decimal[])Array.CreateInstance(typeof(Decimal),
5);

You have specified that the _object_ created is a 5-element array of
decimal. But, the variable you store the object reference in does not
include that information. So as far as the program is concerned, the
information you provided while creating the object is lost as soon as
you assign it to the variable.
So what I in some way don't fully understand is why I have to cast to an
array of decimal when I create an array of decimal like I do in #1

Because the variable has the type "Array" and not "Decimal[]".

Consider this code:

class A { }
class B : A { public void Foo() { } }
class C
{
void Method()
{
A a = new B();

a.Foo(); // not legal

((B)a).Foo(); // legal
}
}

For the same reason you need to cast the variable "a" to the type where
the method "Foo()" is actually found, you also need to cast a plain
"Array" to the specific "Decimal[]" array that tells the compiler that
the object actually stores elements of "Decimal".

Pete
 
Back
Top