Difference between the Length attribute in the Microsoft. VisualBasic. Len function and the system. string class

Source: Internet
Author: User

Environment: vs2003, vs.net
Generally, I want to evaluate the length of a string. There are two methods.
1 is the Microsoft. VisualBasic. Len function; 2 is the Length attribute in the system. string class.
The two functions are similar, but when the string is nothing (C # Is null), the first method returns 0, and the second method returns an error.
As follows:Code:

1 Dim I As Int16
2
3 Dim Stra As   String
4 Stra =   Nothing
5 I = Microsoft. VisualBasic. Len (stra) 'I is 0
6 I = Stra. length' throws an nullreferenceexception
7

Use reflector to check the Microsoft. VisualBasic. Len function. The original function is written as follows:

1 Public   Shared   Function Len () Function   Len ( Byval Expression As   String ) As   Integer
2 If (Expression Is   Nothing ) Then
3 Return   0
4 End   If
5 Return Expression. Length
6 End Function

To use the Length attribute in the system. string class to implement similar functions, You can manually add segmentsProgramTo determine the null value of the string.

If Stra Is   Nothing   Then
I =   0
Else
I = Stra. Length
End   If

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.