473,396 Members | 1,773 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

int n = default(System.Int32);

Hi guys,

what does "default" here mean? What is the MSDN Library Help URL related to
this explanation?
--
Regards
Hardy
Jun 14 '07 #1
6 10356
Hardy Wang wrote:
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/lib...0d(VS.80).aspx.
--
Tom Porterfield
Jun 14 '07 #2
Actually, I saw this type of code from Workflow Foundation auto-generated code.

"Tom Porterfield" wrote:
Hardy Wang wrote:
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/lib...0d(VS.80).aspx.
--
Tom Porterfield
Jun 14 '07 #3
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 Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Tom Porterfield" <tp******@mvps.orgwrote in message
news:et**************@TK2MSFTNGP04.phx.gbl...
Hardy Wang wrote:
>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/lib...0d(VS.80).aspx.
--
Tom Porterfield
Jun 14 '07 #4
Nicholas Paldino [.NET/C# MVP] wrote:
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
Jun 15 '07 #5
Tom Porterfield <tp******@mvps.orgwrote:
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.
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jun 15 '07 #6
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.
> 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();
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.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

Jun 15 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Len | last post by:
While I have no problems selecting different printers to print from my VB.Net app I can't find a way to set the user's system default printer. Unfortunately that's the only way I can get the...
5
by: Barry Edmund Wright | last post by:
Help! Can anyone tell me where the default system.mdw is located? I made a new Workgroup Information File and joined it now I want to join rejoin the default system.mdw file but I can't find...
3
by: DaveB | last post by:
After creating a new dtabase and system.mdw permissions and security can be bypassed by opening the database with the default system.mdw for access 2003. This was not true with access 2000. Any...
3
by: Tom van Stiphout | last post by:
We secured an Access MDB for a client, using the Access 2000 (or above - still trying to find that out) security wizard. I trust the developer who did this, and the screendumps of the process don't...
0
by: Techotsav | last post by:
Hi guys I do have following class structure xxx.RoleManagement : xxx.RMUser. xxx.RMUser : System.web.ui.page These classes are added in solution as different project. Now what i am trying...
2
by: David | last post by:
Hi All: Here's what may be a confusing question. I have an application which has a basic help window that is attached to a Windows application. It uses IE objects to render html text. The user...
0
tbarto
by: tbarto | last post by:
Hello, For some time I have been working on an application where a part of the solutuion is a class that is able to find all the system dialogs ( eg. openFileDialog ) that are currently open....
2
by: TheSteph | last post by:
Hi, does anybody know how I can get the "default system Border Width " ? example : if I draw a TextBox with the default 3D border, what is the with (thikness) of the border ? Thanks for...
3
Megalog
by: Megalog | last post by:
Hey guys- I've implemented this replacement MsgBox (that I found through the Access Developer blog) in the current database I'm working on. It works great for error handling, or anywhere that...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.