[ACCEPTED]-Len() function vs String.Length property; which to choose?-string-length
Because you're using VB.NET, your Strings
can be 9 Nothing
and unless you explicitly check for that, most 8 VB methods, including Len
, will treat it the 7 same as String.Empty
i.e. ""
.
With Reflector you can see 6 Len
is implemented as a null check, returning 5 0
for Nothing
and otherwise returning .Length
, and the 4 JITter will likely in-line the call.
So, if 3 you're using other VB methods, I'd suggest 2 using Len
too, unless you know the String
is not 1 Nothing
or check for Nothing
everywhere.
So according to this:
Len, another classic BASIC 16 function, returns the length of a string. System.String 15 has the Length property that provides the 14 same information. Is one better than the 13 other?
Performance-wise, these two functions 12 show little difference over 1000’s of iterations. There 11 doesn’t appear to be any reason to prefer 10 one over the other in this case plus there 9 is no functional difference. I’m kind of 8 partial to using the property value rather 7 than the VB function since it encourages 6 thinking of .NET strings as objects. However, at 5 the core, it’s really only a personal preference 4 thing.
If you trust their word, then there's 3 your answer. Otherwise, coding up a test 2 and iterating should give you the final 1 answer.
I'm not sure about the specifics of the 10 Len()
method (my language of choice is C#), but 9 I would say definitely go with the Length
property. Length
is 8 a member of the System.String
class, whereas Len()
isn't.
My 7 guess is that Len()
is just a VB shim on top 6 of the Length
property. Someone could probably 5 make the argument that using Len()
is more idiomatic, from 4 a VB point of view. I think I'd prefer 3 to use the property built in to the class, rather 2 than just use a different mechanism just 1 because it's provided by the language.
The Len
method is provided for backwards compatibility 9 with old VB6 (and earlier) non-.NET code. There's 8 nothing technically wrong with using it. It 7 will work, and just as well, at that. But, it's 6 better to use the new .NET way of doing 5 things whenever possible. Outside of getting 4 you more into the ".NET mindset", though, the 3 only real tangible benefit of using String.Length
is 2 that it makes it easier to port the code 1 to other .NET languages in the future.
In addition to @Andrew's post, the Len() is 2 string function from Visual Basic run-time library where as Length
is a property 1 of System.String
class of .net framework API.
Recently I faced problem with my old VB.Net 9 code that was using Len() function. I upgraded 8 my project to Core which was referencing 7 the old VB.net dll file and it was using 6 Len() function. I got run time compatibility 5 error - Method not found: 'Int32 Microsoft.VisualBasic.Strings.Len(System.String)'
I 4 have to change all such old function that 3 Core has deprecated. So I stand by using 2 String.Length over Len() as suggested by 1 Steven Doggart.
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.