int n = default(System.Int32);

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi guys,

what does "default" here mean? What is the MSDN Library Help URL related to
this explanation?
 
Hardy said:
Hi guys,

what does "default" here mean? What is the MSDN Library Help URL related to
this explanation?

That's really (subject line) not the correct usage. Typically default
is used in generic classes where where it is not known if the type is a
value type or a reference type, so it isn't known how to instantiate the
object. In that case, default will return a null if it is a reference
type, and 0 if it is a numeric value type. So the above will set n to
0, but again isn't really the correct usage since the type of n is known.

In MSDN you can see
ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.VisualStudio.v80.en/dv_csref/html/b9daf449-4e64-496e-8592-6ed2c8875a98.htm.

MSDN online see
http://msdn2.microsoft.com/en-us/library/xwth0h0d(VS.80).aspx.
 
Actually, I saw this type of code from Workflow Foundation auto-generated code.
 
Tom,

It's not really the wrong way here, as the compiler allows it. Granted,
it is a little redundant, since you know what the default value in this case
is, but it's definitely not wrong.

Also, the default for a value type is if the default parameterless
constructor was called on it, which zeroes out the structure (so references
are null, other structure fields are zeroed out). It's not just numeric
types.
 
Nicholas said:
Tom,

It's not really the wrong way here, as the compiler allows it.
Granted, it is a little redundant, since you know what the default value
in this case is, but it's definitely not wrong.

I didn't say it was wrong, just not quite right. Just because the
compiler allows it doesn't mean it's right.
Also, the default for a value type is if the default parameterless
constructor was called on it, which zeroes out the structure (so
references are null, other structure fields are zeroed out). It's not
just numeric types.

Didn't mean to infer that it was, sorry you misunderstood.

On the next interview you can ask, explain the difference between the
following:

int x = default(int);
int x = 0;
int x = new int();
 
Tom Porterfield said:
I didn't say it was wrong, just not quite right. Just because the
compiler allows it doesn't mean it's right.

I think it's a pretty reasonable way for a code generator to express
itself. It wants the default value of a type, and knows the name of the
type. It may know whether the type is a value type or a reference type,
but it may not.

In that situation, I can't think of a simpler way of getting the
default value than to use default.

If it were hand-written code I'd agree that it's not a good idea, but
for autogenerated code I think it's fine.
 
Tom,

See inline:
I didn't say it was wrong, just not quite right. Just because the
compiler allows it doesn't mean it's right.

I'm not going to get into what is absolutely right or wrong. The fact
of the matter is that the C# compiler allows the operation, and the current
C# compiler is the most accurate enforcer (even if it is by virtue of being
the only one, or one of a few) of the C# language specification, which I
consider, when it comes to matters of C#, to be absolutely right.
Didn't mean to infer that it was, sorry you misunderstood.

On the next interview you can ask, explain the difference between the
following:

int x = default(int);
int x = 0;
int x = new int();

There was nothing I misunderstood. You had narrowed your definition of
what default does by too much. You stated:

In that case, default will return a null if it is a reference type, and 0 if
it is a numeric value type.

However, what about this type:

public struct MyStruct
{
public string MyString;
}

The above type is not a numeric value type, and there is no defined
behavior according to your original statement (which you subsequently try to
reinforce by being unjustifiably snarky, IMO). My statement, however,
covers the structure above (and ones like it) which are not necessarily
numeric in nature.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top