19

Possible Duplicate:
Howto change what Default(T) returns in C#

print(default(int) == 0) //true

Similarly if I have a custom object, its default value will be null.

print(default(Foo) == null) //true

Can I have a custom value for default(Foo) and not null?

For example, something like this:

public static override Foo default()
{
    return new Foo();
}

This wont compile. Thanks..

Community
  • 1
  • 1
nawfal
  • 70,104
  • 56
  • 326
  • 368
  • 2
    You can specify it in a LINQ query with [`Enumerable.DefaultIfEmpty`](http://msdn.microsoft.com/en-us/library/bb355419.aspx). But not for a single object. @Neverever: Not a duplicate since OP hasn't mentioned collections. – Tim Schmelter Oct 09 '12 at 05:42
  • You can't, and it would be bad if you could. What would `new`ing up an array of that type do? Would it have to invoke the `default` function for every element? Terrible idea - usually you're just going to overwrite them anyway. You can however pretend that the default state is really not zero (for a value type), say you have a field that you want to default to 2: make a property that returns that field plus 2 (and when setting, sets `value - 2`), so zero appears as 2. – harold Oct 09 '12 at 19:10
  • You can however create a static method `Empty()` that returns a *default* instance of the class. Your difficulty is that you are trying to name it with a keyword `default`. It is possible that `@default` might work. – Pieter Geerkens Mar 02 '15 at 06:30

3 Answers3

30

You can't override the default(T) keyword. It is always null for reference types and zero for value types.

More Information

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
dknaack
  • 60,192
  • 27
  • 155
  • 202
13

Doesn't seem like it. From the documentation:

default ... will return null for reference types and zero for numeric value types. For structs, it will return each member of the struct initialized to zero or null depending on whether they are value or reference types.

nneonneo
  • 171,345
  • 36
  • 312
  • 383
8

Frankly, it's not a real answer but a simple mention. If Foo was a struct so you can have something like this:

public struct Foo
{

    public static readonly Foo Default = new Foo("Default text...");

    public Foo(string text)
    {
        mText = text;
        mInitialized = true;
    }

    public string Text
    {
        get
        {
            if (mInitialized)
            {
                return mText;
            }
            return Default.mText;
        }
        set { mText = value; }
    }

    private string mText;
    private bool mInitialized;

}

[TestClass]
public class FooTest
{

    [TestMethod]
    public void TestDefault()
    {
        var o = default(Foo);

        Assert.AreEqual("Default text...", o.Text);
    }

}
m3kh
  • 7,881
  • 2
  • 31
  • 37
  • 1
    I think this is working because, value types (struct) cannot be null. So the object is created. Now when we access the property we can return anything. We are not overriding the default behavior. Even if I use the below statement it will work. public static readonly Foo MyDefault – Joy George Kunjikkuru Jun 19 '15 at 18:28