473,320 Members | 2,133 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 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 2163
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Aaron | last post by:
I know when you compile a .NET app c# or vb the code is converted to MSIL. I was just wondering is it 100% reversible? If so, does that mean someone can steal all your source code just by...
20
by: K.M. Jr. | last post by:
Hi all, Consider this line - char s = "\0"; Does this initialize all array elements to zero ? Thanks.
104
by: Leszek | last post by:
Hi. Is it possible in javascript to operate on an array without knowing how mamy elements it has? What i want to do is sending an array to a script, and this script should add all values from...
2
by: allfyre | last post by:
Ok, to make a long story short, I'm Emitting some MSIL from a c# application. I know that THIS is the MSIL I want to be emitting: //**************************************************************...
9
by: joshc | last post by:
Hi, I have an array defined in one file with an intializer as follows: int arr = {0, 1, 2, 3}; I have a declaration of the array in another file as follows: extern int arr;
2
by: Michael | last post by:
Hi, How to understand the difference between the following three. My understanding is the number in bracket minus one is the max number of chars to store in the char array , right? ...
45
by: VK | last post by:
(see the post by ASM in the original thread; can be seen at <http://groups.google.com/group/comp.lang.javascript/browse_frm/thread/3716384d8bfa1b0b> as an option) As that is not in relevance to...
6
by: Spiro Trikaliotis | last post by:
Hello, in a project, I stumbled upon code like follows (incomplete): #if defined(MINIXVMD) || defined(MINIX_SUPPORT) || defined(__VBCC__) || (defined(__BEOS__) && defined(WORDS_BIGENDIAN)) ||...
2
by: Paul Jackson | last post by:
Here's a brace-enclosed initialiser for a 3D array: int a4 = { { 1, 2, 3, 4, {5, 6, 7 }, }, { {13, 14, 15 }, 17, 18, 19, 20, {21, 22, 23, 24} }}; gcc produces some warnings when compiling...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.