Len() 函数 vs String.Length 属性;选择哪个?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12032312/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 01:34:01  来源:igfitidea点击:

Len() function vs String.Length property; which to choose?

vb.netstringvb6-migrationstring-length

提问by Alan K

I'm making the transition from VB6 to VB.Net (VS 2010) and have a basic rather than expansive understanding of the latter. I obviously have quite a bit of code to... I hesitate to use the word "upgrade" when "port" would be more apt given that the upgrade wizard in past versions of VS may as well have just commented out the code and said "Hey, why don't you re-start from scratch?"

我正在从 VB6 过渡到 VB.Net (VS 2010) 并且对后者有一个基本的而不是广泛的理解。我显然有相当多的代码......我犹豫是否使用“升级”这个词,因为“端口”会更合适,因为过去版本的 VS 中的升级向导也可能刚刚注释掉了代码并说“喂,你为什么不从头开始?”

In one procedure which I'm bringing across the Len()function was used to determine the length of a string variable. That still works in VB.Net (though I imagine that it's actually a call to the Strings.LenMethod), but the other alternative is to just query the .Lengthproperty of the variable.

在我介绍的一个过程中,该Len()函数用于确定字符串变量的长度。这在 VB.Net 中仍然有效(尽管我认为它实际上是对Strings.LenMethod的调用),但另一种选择是仅查询.Length变量的属性。

The question is which to use and why. I've looked through the relevant MSDN pages and all they seem to tell me is that the method/property exists. Nothing is said about performance issues, particularly when loops of large numbers of calls might be involved.

问题是使用哪个以及为什么使用。我浏览了相关的 MSDN 页面,他们似乎告诉我的只是该方法/属性存在。没有提到性能问题,特别是当可能涉及大量调用的循环时。

My question, then, is whether anyone is aware of any tested and confirmed benefit of using one approach over the other, or whether it's merely a question of personal preference. Any pointers on similar situations that I might encounter as I make the progression would also be appreciated though given the Stack Overflow guidelines it's just this one issue that I'm interested in seeing whether there's a specific answer to.

那么,我的问题是,是否有人知道使用一种方法比另一种方法有任何经过测试和确认的好处,或者这是否仅仅是个人偏好的问题。任何关于我在进行过程中可能遇到的类似情况的指示也将不胜感激,尽管鉴于 Stack Overflow 指南,这只是我有兴趣查看是否有特定答案的一个问题。

回答by Mark Hurd

Because you're using VB.NET, your Stringscan be Nothingand unless you explicitly check for that, most VB methods, including Len, will treat it the same as String.Emptyi.e. "".

因为您使用的是 VB.NET,所以您Strings可以Nothing,除非您明确检查,否则大多数 VB 方法(包括Len)会将其视为与String.Emptyie相同""

With Reflector you can see Lenis implemented as a null check, returning 0for Nothingand otherwise returning .Length, and the JITter will likely in-line the call.

使用 Reflector,您可以看到Len实现为空检查,返回0forNothing否则返回.Length,并且 JITter 可能会内联调用。

So, if you're using other VB methods, I'd suggest using Lentoo, unless you know the Stringis not Nothingor check for Nothingeverywhere.

因此,如果您正在使用其他 VB 方法,我建议您也使用Len,除非您知道String不是NothingNothing到处检查。

回答by sewershingle

So according to this:

所以根据这个

Len, another classic BASIC function, returns the length of a string. System.String has the Length property that provides the same information. Is one better than the other?

Performance-wise, these two functions show little difference over 1000's of iterations. There doesn't appear to be any reason to prefer one over the other in this case plus there is no functional difference. I'm kind of partial to using the property value rather than the VB function since it encourages thinking of .NET strings as objects. However, at the core, it's really only a personal preference thing.

Len 是另一个经典的 BASIC 函数,它返回字符串的长度。System.String 具有提供相同信息的 Length 属性。这个比那个好吗?

在性能方面,这两个函数在 1000 次迭代中几乎没有差异。在这种情况下,似乎没有任何理由更喜欢一个而不是另一个,而且没有功能差异。我偏向于使用属性值而不是 VB 函数,因为它鼓励将 .NET 字符串视为对象。然而,归根结底,这真的只是个人喜好的事情。

If you trust their word, then there's your answer. Otherwise, coding up a test and iterating should give you the final answer.

如果您相信他们的话,那么这就是您的答案。否则,编写测试和迭代应该会给你最终的答案。

回答by Andrew Cooper

I'm not sure about the specifics of the Len()method (my language of choice is C#), but I would say definitely go with the Lengthproperty. Lengthis a member of the System.Stringclass, whereas Len()isn't.

我不确定该Len()方法的具体细节(我选择的语言是 C#),但我会说肯定会使用该Length属性。 LengthSystem.String类的成员,而Len()不是。

My guess is that Len()is just a VB shim on top of the Lengthproperty. Someone could probably make the argument that using Len()is more idiomatic, from a VB point of view. I think I'd prefer to use the property built in to the class, rather than just use a different mechanism just because it's provided by the language.

我的猜测是这Len()只是Length属性顶部的 VB 垫片。Len()从 VB 的角度来看,有人可能会认为 using更惯用。我想我更喜欢使用类中内置的属性,而不是仅仅因为它是由语言提供的而使用不同的机制。

回答by adatapost

In addition to @Andrew's post, the Len() is string function from Visual Basic run-time librarywhere as Lengthis a property of System.Stringclass of .net framework API.

除了@Andrew 的帖子之外,Len() 是Visual Basic 运行时库中的字符串函数,其中 asLengthSystem.String.net 框架 API 类的属性。

回答by Steven Doggart

The Lenmethod is provided for backwards compatibility with old VB6 (and earlier) non-.NET code. There's nothing technically wrong with using it. It will work, and just as well, at that. But, it's better to use the new .NET way of doing things whenever possible. Outside of getting you more into the ".NET mindset", though, the only real tangible benefit of using String.Lengthis that it makes it easier to port the code to other .NET languages in the future.

Len提供该方法是为了与旧的 VB6(及更早版本)非 .NET 代码向后兼容。使用它在技术上没有任何问题。它会起作用,而且同样如此。但是,最好尽可能使用新的 .NET 做事方式。但是,除了让您更多地了解“.NET 思维方式”之外,使用的唯一真正切实的好处String.Length是它可以更轻松地将代码移植到未来的其他 .NET 语言中。

回答by Phantom

Recently I faced problem with my old VB.Net code that was using Len() function. I upgraded my project to Core which was referencing the old VB.net dll file and it was using Len() function. I got run time compatibility error - Method not found: 'Int32 Microsoft.VisualBasic.Strings.Len(System.String)'

最近我遇到了使用 Len() 函数的旧 VB.Net 代码的问题。我将我的项目升级到 Core,它引用了旧的 VB.net dll 文件,并且它使用了 Len() 函数。我遇到了运行时兼容性错误 - 找不到方法:'Int32 Microsoft.VisualBasic.Strings.Len(System.String)'

I have to change all such old function that Core has deprecated. So I stand by using String.Length over Len() as suggested by Steven Doggart.

我必须更改 Core 已弃用的所有旧功能。所以我支持按照 Steven Doggart 的建议在 Len() 上使用 String.Length。