Re: default values
From: Nicholas Paldino [.NET/C# MVP] (mvp_at_spam.guard.caspershouse.com)
Date: 05/17/04
- Next message: Sunny: "Re: Systereading.Timer class question"
- Previous message: Mark: "Developing HR applications"
- In reply to: John Wood: "Re: default values"
- Next in thread: John Wood: "Re: default values"
- Reply: John Wood: "Re: default values"
- Reply: Daniel O'Connell [C# MVP]: "Re: default values"
- Messages sorted by: [ date ] [ thread ]
Date: Mon, 17 May 2004 12:06:50 -0400
John,
It's actually quite simple. You can use reflection to determine whether
or not the type derives from ValueType in some way. If it does, then you
can just use the default constructor to generate a value with the bits
zeroed out. If it does not derive from ValueType, then it is a reference
type, and the default value is null.
Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mvp@spam.guard.caspershouse.com
"John Wood" <spam@isannoying.com> wrote in message
news:uuk$IaCPEHA.1392@TK2MSFTNGP09.phx.gbl...
> Yes, that may be true -- but, given an arbitrary value type, how do I
> programmatically retrieve the default value for that type? It can't just
be
> 0 bits, because for a string (for example) that won't work.
>
> I'd have hoped that the boxed equivalent of the value type would have a
> constructor, but GetConstructor returns null.
>
> "Mattias Sjögren" <mattias.dont.want.spam@mvps.org> wrote in message
> news:e4NBVWCPEHA.2468@TK2MSFTNGP11.phx.gbl...
> > John,
> >
> > >My question is, how can I retrieve the default value for a given type?
> >
> > The default is all bits zeroed out, meaning 0 or 0.0 for numeric
> > types, false for bools and null for reference types.
> >
> > For value types, you get the default value when using the default
> > constructor.
> >
> > double d = new double(); // effectively the same as double d = 0.0;
> >
> >
> >
> > Mattias
> >
> > --
> > Mattias Sjögren [MVP] mattias @ mvps.org
> > http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
> > Please reply only to the newsgroup.
>
>
- Next message: Sunny: "Re: Systereading.Timer class question"
- Previous message: Mark: "Developing HR applications"
- In reply to: John Wood: "Re: default values"
- Next in thread: John Wood: "Re: default values"
- Reply: John Wood: "Re: default values"
- Reply: Daniel O'Connell [C# MVP]: "Re: default values"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|