3

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?

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?

4 Answers 4

4

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).

If this isn't the case, then it's better to keep them together.

14
  • I don't think your reasoning is correct, even if your conclusion is. StringBuilder sb; try { sb = GetAStringBuilder(); } finally { Console.WriteLine("in finally"); } if (sb != null) { ... } This does require a separate declaration, but does not require separate initialisation. The reason separate initialisation is required in your example is because the if (sb != null) could otherwise be executed when sb hasn't been set.
    – user743382
    Aug 14, 2012 at 20:37
  • @hvd Yes - that was my point - you'll get issues if you have local scoping about using uninitialized variables unless you initialize "before" your main initialization. Aug 14, 2012 at 20:41
  • The point I'm making is that I do not initialise sb in the code in my previous comment, and do not need to, even though I assign to it in a local scope later. Note the lack of any = null between the StringBuilder sb and the ;.
    – user743382
    Aug 14, 2012 at 20:44
  • @hvd The compiler will complain and provide a warning in your code about using an uninitialized variable (in if (sb != null)), since not all code paths set a value to sb. Aug 14, 2012 at 20:46
  • No, it won't. There is no code path that reaches the if without setting sb. You used catch, I used finally. Either the assignment succeeds, or GetAStringBuilder() throws an exception causing execution to leave the current function.
    – user743382
    Aug 14, 2012 at 20:48
3

There is no advantage whatsoever; the second snippet is more concise and more readable.

2

The advantage of using default comes up only when you develop a generic class that works with parametrized types. 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.

For more details, please see http://msdn.microsoft.com/en-us/library/xwth0h0d%28v=vs.80%29.aspx

3
  • What you say is correct, but not an answer to the question. Regardless of whether T is a value type, T t = default(T); t = new T(); can always be written as T t = new T();.
    – user743382
    Aug 14, 2012 at 20:40
  • Correct, it's just my intention was to provide more general insight on the keyword.
    – Vlad
    Aug 14, 2012 at 20:41
  • Thank you Vlad for the additional insight, I had read that article before posting the question. Aug 16, 2012 at 3:36
2

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 e.g. to zero) or a reference type (initialized to 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.

In .net 3.5 there is one additional convention which you may come across, viz:

var sb = new StringBuilder();

Here the type of sb is inferred from the RHS of the assignment.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.