简体   繁体   English

使用以下语句有什么好处?

[英]What would be the advantage for using the following statements?

Recently, I ran into the following lines: 最近,我遇到了以下几行:

StringBuilder sb = default(StringBuilder);
sb = new StringBuilder();

I would simply write the statement(s) like 我只想简单地写一下这样的陈述

StringBuilder sb = new StringBuilder();

What would be the advantage for using default(StringBuilder) statements? 使用默认(StringBuilder)语句有什么好处?

Based on all the great feedback, I came up with a new question. 基于所有很好的反馈,我提出了一个新问题。

EDIT: Can you see an advantage or disadvantage to doing something like this? 编辑:你能看到做这样的事情的优势或劣势吗? (it does compile) (它确实编译)

var sb = default(StringBuilder);

Again like was mentioned I believe we are looking at whether there is a scope issue or not, but the biggest problem might be objects not getting initialized properly. 再次提到我相信我们正在考虑是否存在范围问题,但最大的问题可能是对象未正确初始化。 what are your thoughts? 你怎么看?

In general, nothing. 一般来说,没什么。 There is no reason to initialize the variable, then set it in the next line of code. 没有理由初始化变量,然后在下一行代码中设置它。 Your second statement is cleaner. 你的第二个陈述更清晰。

There are few reasons to split declaration and assignment. 拆分声明和分配的原因很少。 This typically is only required if there are scoping issues involved, such as trying to use exception handling around the assignment: 这通常仅在涉及范围问题时才需要,例如尝试在赋值周围使用异常处理:

StringBuilder sb = null;

try
{
    // Using a statement that may throw
    sb = GetAStringBuilder();
}
catch
{
    //...
}

// The compiler will warn about a potentially 
// uninitalized variable here without the default assignment
if (sb != null)  
{
    //...

In this case, you need to split the two because your doing the assignment within a local scope (the try ). 在这种情况下,您需要拆分两个,因为您在本地范围内执行赋值( try )。

If this isn't the case, then it's better to keep them together. 如果不是这种情况,那么最好将它们保持在一起。

There is no advantage whatsoever; 没有任何优势; the second snippet is more concise and more readable. 第二个片段更简洁,更易读。

The advantage of using default comes up only when you develop a generic class that works with parametrized types. 只有在开发使用参数化类型的泛型类时,才会出现使用default的优点。 Sometimes, it is not known whether the type is a reference type or a value type or a struct. 有时,不知道类型是引用类型还是值类型或结构。 The default keyword returns null for reference types and 0 for numeric value types. default参数类型的引用类型返回null ,数值类型返回0

For more details, please see http://msdn.microsoft.com/en-us/library/xwth0h0d%28v=vs.80%29.aspx 有关详细信息,请参阅http://msdn.microsoft.com/en-us/library/xwth0h0d%28v=vs.80%29.aspx

The default keyword is often used with the initialization of generic types, where one cannot be certain whether we are dealing with a value type (initialized eg to zero) or a reference type (initialized to null). default关键字通常与泛型类型的初始化一起使用,其中无法确定我们是处理值类型(初始化为例如零)还是引用类型(初始化为null)。 As per other answers, in the example you provided, there is little purpose either initializing StringBuilder and reassigning it immediately, nor using the default keyword. 根据其他答案,在您提供的示例中,无论是初始化StringBuilder还是立即重新分配它,也没有使用default关键字。

In .net 3.5 there is one additional convention which you may come across, viz: 在.net 3.5中,您可能会遇到一个额外的约定,即:

var sb = new StringBuilder();

Here the type of sb is inferred from the RHS of the assignment. 这里sb的类型是从赋值的RHS推断出来的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM