In the code ....
void OneWay() { System.Data.SqlClient.SqlDataReader reader = null; int i = reader.GetInt32(0); } void OtherWay() { System.Data.SqlClient.SqlDataReader reader = null; int i = (int)reader[0]; }
In IL
.method private hidebysig instance void OneWay() cil managed { .maxstack 2 .locals init ( [0] class [System.Data]System.Data.SqlClient.SqlDataReader reader, [1] int32 i) L_0000: nop L_0001: ldnull L_0002: stloc.0 L_0003: ldloc.0 L_0004: ldc.i4.0 L_0005: callvirt instance int32 [System.Data]System.Data.Common.DbDataReader::GetInt32(int32) L_000a: stloc.1 L_000b: ret } .method private hidebysig instance void OtherWay() cil managed { .maxstack 2 .locals init ( [0] class [System.Data]System.Data.SqlClient.SqlDataReader reader, [1] int32 i) L_0000: nop L_0001: ldnull L_0002: stloc.0 L_0003: ldloc.0 L_0004: ldc.i4.0 L_0005: callvirt instance object [System.Data]System.Data.Common.DbDataReader::get_Item(int32) L_000a: unbox.any int32 L_000f: stloc.1 L_0010: ret }
So, IL is different, but I doubt that there is any noticeable difference between them. Perhaps after a million iterations you will see the difference, but hardly.
source share