Using the string. Split () method, let's talk about the differences between the VB. NET compiler and the C # compiler!

Source: Internet
Author: User
Tags mscorlib

The problem originated from a post in the Microsoft official forum. He provided a piece of code written in VB. NET:

Dim stralltext As String = My. Computer. FileSystem. ReadAllText ("c: \ magic.txt ")

Dim StrLines () As String = stralltext. Split (ControlChars. CrLf)

Let's take a look at the second line of the Split method call, ControlChars. crLf is a string type, but the first parameter in the reload of the Split method is the reload Implementation of the string type. Because C # code is frequently written. NET does not know much about it. I take it for granted that this Code cannot be compiled. In fact, the following two compilation errors may occur when writing similar code in C:

Error 1: The overload method that best matches "string. Split (params char [])" has some invalid parameters.
Error 2 parameter "1": cannot be converted from "string" to "char []"

 

I used the VB. NET compiler again to compile this code. The behavior of the VB compiler was indeed unexpected !, After compilation, my intuition tells me that the VB. NET compiler converts string to a char array. You can see the generated IL code:

. Method public static void Main () cel managed
{
. Entrypoint
. Custom instance void [mscorlib] System. STAThreadAttribute:. ctor () = (01 00 00 00)
// Code size 44 (0x2c)
. Maxstack 4
. Locals init ([0] string stralltext,
[1] string [] StrLines,
[2] char [] VB $ t_array $ S0)
IL_0000: nop
IL_0001: call class ConsoleApplication1.My. MyComputer ConsoleApplication1.My. MyProject: get_Computer ()
IL_0006: callvirt instance class [Microsoft. VisualBasic] Microsoft. VisualBasic. MyServices. FileSystemProxy [Microsoft. VisualBasic] Microsoft. VisualBasic. Devices. ServerComputer: get_FileSystem ()
IL_000b: ldstr "c: \ magic.txt"
IL_0010: callvirt instance string [Microsoft. VisualBasic] Microsoft. VisualBasic. MyServices. FileSystemProxy: ReadAllText (string)
IL_0015: stloc.0
IL_0016: ldloc.0
IL_0017: ldc. i4.1
IL_0018: newarr [mscorlib] System. Char // The VB compiler automatically constructs a Char array.
IL_001d: stloc.2
IL_001e: ldloc.2
IL_001f: ldc. i4.0
IL_0020: ldc. i4.s 97
IL_0022: stelem. i2
IL_0023: ldloc.2
IL_0024: callvirt instance string [] [mscorlib] System. String: Split (char []) // The String. Split (char []) overload is called here.
IL_0029: stloc.1
IL_002a: nop
IL_002b: ret
} // End of method Module1: Main

This section of IL code proves my first inference, VB. the NET compiler indeed constructs a char array, which is different from the C # compiler. The C # compiler does not implicitly convert string to char [].

Maybe VB. NET programmers are very familiar with this phenomenon (don't laugh! For VB. NET compiler is not very familiar), but C # cannot be used in this way. In order not to make C # programmers confused during language interoperability, I still sincerely hope VB. NET programmers can explicitly construct Char arrays!

Related Article

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.