English 中文(简体)
What is the use of `default` keyword in C#?
原标题:
  • 时间:2009-11-13 05:43:35
  •  标签:
  • c#
  1. What is the use of default keyword in C#?
  2. Is it introduced in C# 3.0 ?
最佳回答

The default keyword is contextual since it has multiple usages. I am guessing that you are referring to its newer C# 2 meaning in which it returns a type s default value. For reference types this is null and for value types this a new instance all zero d out.

Here are some examples to demonstrate what I mean:

using System;

class Example
{
    static void Main()
    {
        Console.WriteLine(default(Int32)); // Prints "0"
        Console.WriteLine(default(Boolean)); // Prints "False"
        Console.WriteLine(default(String)); // Prints nothing (because it is null)
    }
}
问题回答

You can use default to obtain the default value of a Generic Type as well.

public T Foo<T>()
{
    .
    .
    .
    return default(T);
}

The most common use is with generics; while it works for "regular" types (i.e. default(string) etc), this is quite uncommon in hand-written code.

I do, however, use this approach when doing code-generation, as it means I don t need to hard-code all the different defaults - I can just figure out the type and use default(TypeName) in the generated code.

In generics, the classic usage is the TryGetValue pattern:

public static bool TryGetValue(string key, out T value) {
    if(canFindIt) {
        value = ...;
        return true;
    }
    value = default(T);
    return false;
}

Here we have to assign a value to exit the method, but the caller shouldn t really care what it is. You can contrast this to the constructor constraint:

public static T CreateAndInit<T>() where T : ISomeInterface, new() {
    T t = new T();
    t.SomeMethodOnInterface();
    return t;
}

The default keyword has different semantics depending on its usage context.

The first usage is in the context of a switch statement, available since C# 1.0:
http://msdn.microsoft.com/en-us/library/06tc147t(VS.80).aspx

The second usage is in the context of generics, when initializing a generic type instance, available since C# 2.0:
http://msdn.microsoft.com/en-us/library/xwth0h0d(VS.80).aspx

"default" keyword (apart from switch-case) helps you initialize the instance of an object like class, list and more types. It is used because of its generic property where it helps you to assign the type s default value when you do not know its value in advance as a way to avoid mistakes in your further(future) code.

Echoing and emphasizing it s use in generics and little else other than code-generation.

If you have to initialize to default (already suspiciously smelly in my book) be clear. Just do it.





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签