C# produces better IL than C++ 
Author Message
 C# produces better IL than C++

Sample C#
String[] sTemp = new String[1];
sTemp[0] = "hello world";
produces
  IL_005d:  newarr     [mscorlib]System.String
  IL_0062:  stloc.1
  IL_0063:  ldloc.1
  IL_0064:  ldc.i4.0
  IL_0065:  ldstr      "hello world"
  IL_006a:  stelem.ref

Managed C++
Source:
String * saMails[] = new String*[1];
saMails->set_Item(0, new String("hello world");
produces:
  IL_0008:  newarr     [mscorlib]System.String
  IL_000d:  stloc.3
  IL_000e:  ldloc.3
  IL_000f:  ldc.i4.0
  IL_0010:  ldsflda    valuetype $ArrayType$0x14f3579d

  IL_0015:  newobj     instance void [mscorlib]System.String::.ctor(int8*)
  IL_001a:  callvirt   instance void
[mscorlib]System.Collections.IList::set_Item(int32,

object)

Question: Am I wrong saying this? If so, what options do I need to set or
what code style change do I need?
--



Thu, 10 Mar 2005 05:36:39 GMT  
 C# produces better IL than C++

Got one clue already to use the S prefix for strings
C++

saMails->set_Item(0, S"Hello World");
IL:
  IL_000c:  ldstr      "Hello World"
  IL_0011:  callvirt   instance void
[mscorlib]System.Collections.IList::set_Item(int32,

object)

STILL why does C# use this:
  IL_000a:  ldstr      "Hello WOrld"
  IL_000f:  stelem.ref

while C++ uses this
  IL_0011:  callvirt   instance void
[mscorlib]System.Collections.IList::set_Item(int32,

object)



Thu, 10 Mar 2005 05:57:38 GMT  
 C# produces better IL than C++
Egbert,

Quote:
> Got one clue already to use the S prefix for strings
> C++

> saMails->set_Item(0, S"Hello World");
> IL:
>   IL_000c:  ldstr      "Hello World"
>   IL_0011:  callvirt   instance void
> [mscorlib]System.Collections.IList::set_Item(int32,

> object)

> STILL why does C# use this:
>   IL_000a:  ldstr      "Hello WOrld"
>   IL_000f:  stelem.ref

> while C++ uses this
>   IL_0011:  callvirt   instance void
> [mscorlib]System.Collections.IList::set_Item(int32,
> object)

Because that's what you're telling it to do. You are using the Item property
of the Array class directly, which is inherited from IList; hence the call
to IList::set_Item. If you were using array syntax, it would do the same as
C#:

String * saMails[] = new String*[1];
saMails[0] = S"hello world";

 IL_0003:  newarr     [mscorlib]System.String
  IL_0008:  stloc.0
  IL_0009:  ldloc.0
  IL_000a:  ldc.i4.0
  IL_000b:  ldstr      "hello world"
  IL_0010:  stelem.ref

--
Tomas Restrepo



Thu, 10 Mar 2005 06:17:48 GMT  
 C# produces better IL than C++
Quote:

> Egbert,
>> Got one clue already to use the S prefix for strings
>> C++

>> saMails->set_Item(0, S"Hello World");
>> IL:
>>   IL_000c:  ldstr      "Hello World"
>>   IL_0011:  callvirt   instance void
>> [mscorlib]System.Collections.IList::set_Item(int32,

>> object)

>> STILL why does C# use this:
>>   IL_000a:  ldstr      "Hello WOrld"
>>   IL_000f:  stelem.ref

>> while C++ uses this
>>   IL_0011:  callvirt   instance void
>> [mscorlib]System.Collections.IList::set_Item(int32,
>> object)

> Because that's what you're telling it to do. You are using the Item
> property of the Array class directly, which is inherited from IList;
> hence the call to IList::set_Item. If you were using array syntax, it
> would do the same as C#:

Thanks,
for some strange reason, it did not work but now it does...


Thu, 10 Mar 2005 06:31:52 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Compiler produced IL code

2. How good is Managed C++ as a glue between C# and C++

3. Need Help!!Convert IL code to C#

4. C# Sample thank uses IL

5. C# COM objects and IL code question

6. transform IL code to C#

7. Way to convert IL code to C#

8. C# vs C++ Which is the better choice?

9. Best Practices Question - Interacting with VB, C#, C++

10. Looking for good textbook for CS course

11. Good book on Microsoft Visual Studio Visual C++, C#, VB

12. Good CS Programs

 

 
Powered by phpBB® Forum Software