views:

176

answers:

6

lets say I have the following

public class A 
{
   private string _someField;
   public string SomeField { get { return _someField; } }
}

For some reason I am checking the default of this class and I would like to set the default for a class, just like a default of type int is 0, I would like in the above class for my default of Somefield to be "hello";

int i = default(int); // i is 0
A myClass = default(A);
string s = myClass.SomeField; // s is hello

This is more just for my own theoretical satisfaction rather than practical application. Just wondering.

+5  A: 

No. The default for classes (reference types) is null and cannot be overloaded.

Sean Bright
+1. default(T) == null where T : class
Mehrdad Afshari
A: 

This should do the job:

public class A 
{
   private string _someField = "hello";
   public string SomeField { get { return _someField; } }
}

Now when you create an instance of that class, the initial value of someField will be hello.

[Edit: This doesn't do quite what you want. Like others have noted in the comments, default(T) where T is a class will always result in null.

Instead you would create the class normally instead of using the 'default' keyword.

A myClass = new A();
string defaultValue = myClass.SomeField // This will be set to "hello" by default.

]

Simon P Stevens
default(T) is always null, that's the value after calling the default constructor.
Mehrdad Afshari
This will cause a null reference exception when myClass is dereferenced.
Stefan Moser
-1 If it were a struct perhaps but a default of T where T : class is always null.
Andrew Hare
@Mehrdad: Yes, fair point, this wouldn't quite work with your existing code. You would just create the class normally - not using the keyword 'default' and the value of someField would always be set to "hello" when the class is first created.
Simon P Stevens
@Simon P Stevens: While your statement is true it really has nothing to do with the question.
Andrew Hare
@Simon P Stevens: In order for that to work you would have to constrain the generic type parameter with "new()" so that you could invoke the type's default constructor.
Andrew Hare
I've edited to define the usage more clearly. The way I read the question is that the OP wanted a way of having a default value for his class members, yes it isn't done in the way he originally wanted, but this is an appropriate alternative.
Simon P Stevens
@Andrew: woah, have I really misunderstood the question, where does it mention generics? He just uses the letter 'T' I don't think he wants a generic. Am I wrong?
Simon P Stevens
@Simon: Oops! No I misread it - you are correct, the OP doesn't seem to mention generics anywhere!
Andrew Hare
@Simon: I have removed my downvote as you have corrected the error and what your question now says is correct.
Andrew Hare
@Andrew: Appreciate it, but I think as it turns out I was in the wrong - Brandon was clearly asking about the "semantics of default" as he mentions somewhere else. I'll leave this here in case its useful to someone else.
Simon P Stevens
+5  A: 

You cannot change what default(T) is for a T. It is always null for reference types, and the 'empty' value for value types (ie. for a struct, all members are at their default, uninitalized values).

Jonathan
+1 Nicely done.
Andrew Hare
A: 

In case of classes (reference types) the default keyword doesn't do anything for the members of the class, it just sets the whole reference to null

bashmohandes
+3  A: 

There is no way of overloading default(T).

To me, it really sounds like you're asking for non-nullable reference types which don't yet exist in .NET. Have a look here for an implementation: http://msmvps.com/blogs/jon_skeet/archive/2008/10/06/non-nullable-reference-types.aspx

Stefan Moser
That is an interesting way around it Stefan, given that a struct is non nullable.
Brandon Grossutti
A: 

While default() will always return null, you could use where to specify that the class must contain a parameterless constructor, so you can call new on it.

void SomeMethod<T>(T something) where T : new()
    {
        T newObject = new T();
    }
Paul