473,839 Members | 1,532 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 10384
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.ht m.

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.ht m.

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.co m

"Tom Porterfield" <tp******@mvps. orgwrote in message
news:et******** ******@TK2MSFTN GP04.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.ht m.

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.co m>
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.co m

Jun 15 '07 #7

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

Similar topics

0
1247
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 legacy app that I call to print properly. If anybody has suggestions or code sniplets, please respond. Thanks,
5
12997
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 it. I did a search on the entire harddrive for system.mdw and it returned nothing.
3
1840
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 thoughts
3
1683
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 give any indication he missed a step, or got an error, etc. On several machines in our office, if you try to open the database without the special system.mdw, you correctly get an error "You do not have the necessary permissions...". With that...
0
2834
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 is to change .aspx page's base class from default system.web.ui.page to xxx.rolemanagement. But it is throwing following
2
2466
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 isn't aware that the basic help window is an IE window since it has no controls on it. Within that window, however, are links to "advanced help" pages which I want to open in a new (full-size) window in the DEFAULT system browser. The problem is...
0
1102
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. Dependently on the system regional settings, the captions of such dialogs differ (e.g "Open" for en-En and "Otwieranie" for pl-Pl ). I neet do know what is the exact caption for the current system. To be more detailed I need to find a solution that...
2
4698
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 your help ! Steph3.
3
2545
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 I'm calling on a message box in VBA. Basically all you do is install a few modules, one form, and replace 'MsgBox' with the new 'Box' function wherever it's to be used. But it's not working for native system messages, and I'm wondering if any of...
0
9854
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9696
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10903
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10584
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9425
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7015
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5865
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4482
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4063
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.