String S = new string ("XYZ"); how many string instances are created?

Source: Internet
Author: User
Tags dotnet mscorlib

This is a very interesting and in-depth topic in the ghost broadcaster dotnetw.baodian ().pdf ).pdf.

String S = new string ("XYZ"); how many string instances are created?

The teacher told the students that the answer is two: one is "XYZ" and the other is a reference object pointing to "XYZ.

It seems to be correct. It makes sense. Yes, "XYZ" is a string, and the DOTNET string (in fact, Java is also) has a mechanism such as intern.

Sorry, the teacher of Chuanzhi did not expect that the student or company who asked this question was too bad. The question is wrong, and the answer is wrong.

You can use Visual Studio to create a console.ProgramTry it. Can it be compiled ?!

As for why the answer is not correct, let's take a look at this very detailed explanation, although the author explains it for the Java language.

Http://www.iteye.com/topic/774673

Why can I write in Java like this? Because Java has such ConstructorString(String original) but the string class in DOTNET does not.

Slightly modified to enable compilation

Static void main (string [] ARGs) {char [] chars = {'w', 'O', 'R', 'D '}; string S2 = new string (chars); string S3 = new string (chars); int I = s2.length; Int J = s3.length; console. writeline (I + J );}

If we use ilspy, the result is as follows. Note that only newobj is called twice, for S2 and S3 respectively.

. Method private hidebysig static
Void main (
String [] ARGs
) Pencil
Managed
{
// Method begins at RVA 0x2058
// Code size 57
(0x39)
. Maxstack 3
. Entrypoint
. Locals Init (
[0] char []
Chars,
[1] string S2,
[2] string S3,
[3] int32 I,
[4] int32 J
)
Il_0000: LDC. i4.4
Il_0001: newarr [mscorlib] system. Char
Il_0006: DUP
Il_0007: ldtoken
Field
Int64' <privateimplementationdetails> {06fae1c3-0e45-4ae7-a401-b8a7d1eff5d6} ':' $ method0x6000001-1'
Il_000c: Call
Void [mscorlib] system. runtime. compilerservices. runtimehelpers: initializearray (class [mscorlib] system. array, valuetype [mscorlib] system. runtimefieldhandle)
Il_0011: stloc.0

Il_0012: ldloc.0
Il_0013: newobj instance
Void [mscorlib] system. String:. ctor (char [])
Il_0018: stloc.1

Il_0019: ldloc.0
Il_001a: newobj instance
Void [mscorlib] system. String:. ctor (char [])
Il_001f: stloc.2

Il_0020: ldloc.1
Il_0021: callvirt instance
Int32 [mscorlib] system. String: get_length ()
Il_0026: stloc.3

Il_0027: ldloc.2
Il_0028: callvirt instance
Int32 [mscorlib] system. String: get_length ()
Il_002d: stloc. S J

Il_002f: ldloc.3
Il_0030: ldloc. S J
Il_0032: add
Il_0033: Call
Void [mscorlib] system. Console: writeline (int32)
Il_0038: Ret
} // End
Of method program: Main

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.