何时使用"尝试"在方法的名字吗? [英] When to use "Try" in a method name?
问题描述
我们上如果方法名称以尝试启动时,它的意思与我们的同事讨论
We were discussing with our coworkers on what it means if the method name starts with "Try".
有以下意见:
- 使用试试的时候,方法可以返回空值。
- 使用试试的时候,方法不会抛出一个例外。
什么是官方定义?什么是尝试说,在方法的名字吗?有没有关于这一些公文写作?
What is the official definition? What does "Try" say in the method name? Is there some official writing about this?
推荐答案
这就是所谓的的TryParse 模式,并已微软记录。该官方异常和性能MSDN页面说:
This is known as the TryParse pattern and has been documented by Microsoft. The official Exceptions and Performance MSDN page says:
考虑针对可能引发的常见方案,以避免与异常的性能问题异常成员的TryParse模式。
Consider the TryParse pattern for members that may throw exceptions in common scenarios to avoid performance problems related to exceptions.
因此,如果你有代码,其中一个经常使用的情况下,就意味着它可能会抛出异常(如解析一个int),在的TryParse 格局是有道理的。
Thus if you have code for which a regular use case would mean that it might throw an exception (such as parsing an int), the TryParse pattern makes sense.
这篇关于何时使用"尝试"在方法的名字吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!