Re: default values

Tech-Archive recommends: Fix windows errors by optimizing your registry

From: Nicholas Paldino [.NET/C# MVP] (mvp_at_spam.guard.caspershouse.com)
Date: 05/17/04


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


Relevant Pages

  • Re: default values
    ... Isn't there a method somewhere in the framework that just returns the ... > or not the type derives from ValueType in some way. ... If it does not derive from ValueType, ... >> constructor, but GetConstructor returns null. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: default values
    ... You can use reflection to determine ... > or not the type derives from ValueType in some way. ... If it does not derive from ValueType, ... >> constructor, but GetConstructor returns null. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: default values
    ... >> or not the type derives from ValueType in some way. ... then you>> can just use the default constructor to generate a value with the bits ... If it does not derive from ValueType, then it is a reference>> type, and the default value is null. ... false for bools and null for reference types. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: default values
    ... reflection and call through reflection. ... >> or not the type derives from ValueType in some way. ... If it does not derive from ValueType, then it is a reference>> type, and the default value is null. ... >>> I'd have hoped that the boxed equivalent of the value type would have a>>> constructor, ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Types system in .NET
    ... In some circumstances (such as array creation) you'd either have to ... have to accept that the constructor wasn't always called. ... ValueType is indeed a reference type. ...
    (microsoft.public.dotnet.framework)