tags:

views:

190

answers:

6

Do all objects have a default value?

Example;

Guid guid = default(Guid);

Gives; 00000000-0000-0000-0000-000000000000

DateTime dt = default(DateTime);

Gives; 01/01/0001 00:00:00

Is this true for all objects, do all objects give some value as a default? I'm right in assuming yes...?

A: 

The default for reference types is null.

Yuriy Faktorovich
Why the downvote?
Yuriy Faktorovich
A: 

Yes. Reference types default to null, and Value types - like those you mention - default to specific values equating to zero.

Stuart Dunkeld
+5  A: 

"return null for reference types and zero for numeric value types."

http://msdn.microsoft.com/en-us/library/xwth0h0d%28v=VS.80%29.aspx

Might help explain a little more.

Pino
Out of curiousness: How come `default(DateTime)` is `01/01/0001 00:00:00` and not `00/00/0000 00:00:00`?
Markus
@Markus: Because there was no year zero, there is no month zero and there is no day zero of any month! What would 0/0/0000 even *mean*? (Incidentally, in JavaScript if you ask for "day 0" of a month you get the last day of the previous month. This is confusing and weird; asking for day zero of a month should be an *error*.)
Eric Lippert
@Eric: Thank you, Captain Obvious. :D *scnr* My question is why `default()` yields this result since it `[returns] zero for numeric value types`.
Markus
@Markus: A DateTime is implemented as *the number of 'ticks' since midnight of January 1st, 1AD*. (A 'tick' is 100 ns if I recall correctly.) Zero ticks after midnight, January 1st, 1AD is midnight, January 1st, 1AD. Captain Obvious strikes again! Off to help others!
Eric Lippert
@Eric: Ah, interesting. So it's initialized to zero indeed. Well, I guess Captain Obvious saved the day! :) - Jokes aside: Thanks alot!
Markus
A: 

Yes you can get it by calling

default(T)     

when T is your type (value-type or reference-type)

Carlos Muñoz
I'm guessing he already knew that, since he is using it in the code sample he posted!
fearofawhackplanet
Is my code wrong? Is it misleading somehow? i don't think I deserve the -1. No voting would be ok
Carlos Muñoz
+1  A: 

Yes. Value types will have default value as defined. Numeric value is default to 0, and boolean is default to false. See here for more details http://msdn.microsoft.com/en-us/library/83fhsxwc.aspx

Reference type will be default to null.

Fadrian Sudaman
why -1? The link list the appropriate default value for different type for C# as per the question asked. Please enlighten
Fadrian Sudaman
+9  A: 

Do all objects have a default value?

Absolutely not. For example, the string "abc" is an object, but it does not have a "default value". The number 12 is an object, but it does not have a "default value".

However, all types have a default value. Remember, objects are instances of types; objects exist at runtime. Types are a compile-time concept. Do not confuse types with objects; they are as different as the string "The New York Times" and an actual copy of today's New York Times.

Values that may be stored in a variable of reference type are either references to objects or null. Hence the name "reference type": a value of a variable of reference type is a reference (or null).

Values that may be stored in a variable of value type are objects that are the values of that type. Hence the name "value type" - the value of a variable of value type is a value.

(I omit pointer types from the discussion; for our purposes, assume that all pointer types are logically the same as the value type IntPtr.)

The default value of any reference type is the null reference value.

The default value of any numeric value type - int, decimal, and so on - is the zero of that type. (Types that support multiple representations of zero, like float, choose the positive zero.) The default value of bool is false. The default value of any nullable value type is the null value of that value type.

The default value of any other value type is recursively defined as the value of that type formed by setting all of the fields of the type to their default values.

Is that clear?

Eric Lippert
@Eric: "The default value of any other value type..." I noticed you said "any other value type" instead of saying "a `struct`". Is there any other value type left besides `struct` ?
Brian
@Brian: Nope. I could have said "struct", but that would have been not parallel with the structure of the other sentences in the list.
Eric Lippert