Herfried,
| > Which although may be shorter (both source wise & IL wise), may be more
| > obscure for some developers.
| Mhm. I am curious what the overhead of calling 'ToCharArray' is
Actually the "overhead" is in the array initializer, not the ToCharArray!
;-)
In VB 2005 (Debug & Release builds) the IL for the ToCharArray is
significantly shorter, 3 lines of IL. While the IL for the Array initializer
is 70+ lines of IL building the array itself. Both initialization code is
placed in a shared constructor...
I use both depending on the problem at hand, I consider it largely personal
preference, some problem domain... I would only worry about performance
overhead, once profiling indicated that there was a performance problem...
Given:
Public Class Constants
Private Shared hexDigits1 As Char() =
"0123456789ABCDEF".ToCharArray()
Private Shared hexDigits2 As Char() = {"0"c, "1"c, "2"c, "3"c, "4"c,
"5"c, "6"c, "7"c, "8"c, "9"c, "A"c, "B"c, "C"c, "D"c, "E"c, "F"c}
End Class
Produces the following IL:
..method private specialname rtspecialname static
void .cctor() cil managed
{
// Code size 118 (0x76)
.maxstack 3
.locals init ([0] char[] VB$t_array$S0)
.language '{3A12D0B8-C26C-11D0-B442-00A0244A1DD2}',
'{994B45C4-E6E9-11D2-903F-00C04FA302A1}',
'{5A869D0B-6611-11D3-BD2A-0000F80849BD}'
// Source File 'C:\...\MainModule.vb'
//000104: Private Shared hexDigits1 As Char() =
"0123456789ABCDEF".ToCharArray()
IL_0000: ldstr "0123456789ABCDEF"
IL_0005: callvirt instance char[]
[mscorlib]System.String::ToCharArray()
IL_000a: stsfld char[] ....MainModule/Constants::hexDigits1
//000105: Private Shared hexDigits2 As Char() = {"0"c, "1"c, "2"c,
"3"c, "4"c, "5"c, "6"c, "7"c, "8"c, "9"c, "A"c, "B"c, "C"c, "D"c, "E"c,
"F"c}
IL_000f: ldc.i4.s 16
IL_0011: newarr [mscorlib]System.Char
IL_0016: stloc.0
IL_0017: ldloc.0
IL_0018: ldc.i4.0
IL_0019: ldc.i4.s 48
IL_001b: stelem.i2
IL_001c: ldloc.0
IL_001d: ldc.i4.1
IL_001e: ldc.i4.s 49
IL_0020: stelem.i2
IL_0021: ldloc.0
IL_0022: ldc.i4.2
IL_0023: ldc.i4.s 50
IL_0025: stelem.i2
IL_0026: ldloc.0
IL_0027: ldc.i4.3
IL_0028: ldc.i4.s 51
IL_002a: stelem.i2
IL_002b: ldloc.0
IL_002c: ldc.i4.4
IL_002d: ldc.i4.s 52
IL_002f: stelem.i2
IL_0030: ldloc.0
IL_0031: ldc.i4.5
IL_0032: ldc.i4.s 53
IL_0034: stelem.i2
IL_0035: ldloc.0
IL_0036: ldc.i4.6
IL_0037: ldc.i4.s 54
IL_0039: stelem.i2
IL_003a: ldloc.0
IL_003b: ldc.i4.7
IL_003c: ldc.i4.s 55
IL_003e: stelem.i2
IL_003f: ldloc.0
IL_0040: ldc.i4.8
IL_0041: ldc.i4.s 56
IL_0043: stelem.i2
IL_0044: ldloc.0
IL_0045: ldc.i4.s 9
IL_0047: ldc.i4.s 57
IL_0049: stelem.i2
IL_004a: ldloc.0
IL_004b: ldc.i4.s 10
IL_004d: ldc.i4.s 65
IL_004f: stelem.i2
IL_0050: ldloc.0
IL_0051: ldc.i4.s 11
IL_0053: ldc.i4.s 66
IL_0055: stelem.i2
IL_0056: ldloc.0
IL_0057: ldc.i4.s 12
IL_0059: ldc.i4.s 67
IL_005b: stelem.i2
IL_005c: ldloc.0
IL_005d: ldc.i4.s 13
IL_005f: ldc.i4.s 68
IL_0061: stelem.i2
IL_0062: ldloc.0
IL_0063: ldc.i4.s 14
IL_0065: ldc.i4.s 69
IL_0067: stelem.i2
IL_0068: ldloc.0
IL_0069: ldc.i4.s 15
IL_006b: ldc.i4.s 70
IL_006d: stelem.i2
IL_006e: ldloc.0
IL_006f: stsfld char[] ....MainModule/Constants::hexDigits2
IL_0074: nop
IL_0075: ret
} // end of method Constants::.cctor
| but here
| it's christmas eve.
Merry Christmas! Its Christmas Eve here also, well almost its 2:00 in the
afternoon as I write this... We'll start the celebrations in a few hours...
--
Hope this helps
Jay [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley -
http://www.tsbradley.net
"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:uI**************@TK2MSFTNGP09.phx.gbl...
| "Jay B. Harlow [MVP - Outlook]" <Ja************@tsbradley.net> schrieb:
| > FWIW: The statement could also have been written:
| >
| > Private Shared hexDigits As Char() =
| > "0123456789ABCDEF".ToCharArray()
| >
| > Which although may be shorter (both source wise & IL wise), may be more
| > obscure for some developers.
|
| Mhm. I am curious what the overhead of calling 'ToCharArray' is -- but
here
| it's christmas eve.
|
| --
| M S Herfried K. Wagner
| M V P <URL:http://dotnet.mvps.org/>
| V B <URL:http://classicvb.org/petition/>
|