471,627 Members | 1,356 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,627 software developers and data experts.

[MSIL Generation] Local Array Initialisers

After the following has been compiled, the number literal values are stored
in a special class field:

class Test {
static void Main() {
int[] arr = new int[] { 1, 2, 3 };
System.Console.WriteLine(arr[0]);
}
}

This is not true for the following, wherein the literal values are included
within the MSIL code for the method:

class Test {
static void Main() {
string[] arr = new string[] { "one", "two", "three" };
System.Console.WriteLine(arr[0]);
}
}

Does anyone know the rationale behind this?
Dec 3 '05 #1
1 2124
Does anyone know the rationale behind this?


The strings, being reference types, must be allocated from the GC
heap. The string[] only stores references to each string object and
those references can't be known at compile time.

For ints and other simple value types the array can be quickly
initialized by storing the values sequentially in the module and
basically doing a memcpy to the array with
RuntimeHelpers.InitializeArray.
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Dec 3 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by Aaron | last post: by
20 posts views Thread by K.M. Jr. | last post: by
104 posts views Thread by Leszek | last post: by
2 posts views Thread by Michael | last post: by
6 posts views Thread by Spiro Trikaliotis | last post: by
2 posts views Thread by Paul Jackson | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.