Re: default values
From: Nicholas Paldino [.NET/C# MVP] (mvp_at_spam.guard.caspershouse.com)
Date: 05/17/04
- Next message: Dave: "Re: Enum data type question?"
- Previous message: Dmitriy Lapshin [C# / .NET MVP]: "Re: Wrapping text in DataGridTextBoxColumn"
- In reply to: John Wood: "Re: default values"
- Next in thread: John Wood: "Re: default values"
- Reply: John Wood: "Re: default values"
- Messages sorted by: [ date ] [ thread ]
Date: Mon, 17 May 2004 12:17:44 -0400
John,
Yes, actually it would. With Guid and DateTime, since they are
structures, they have default constructors, which you can get through
reflection and call through reflection (once you have the type).
With a string, it is not a value type, so the default value for a string
is null, which the algorithm that I detailed in my previous post will return
to you.
--
- Nicholas Paldino [.NET/C# MVP]
- mvp@spam.guard.caspershouse.com
"John Wood" <spam@isannoying.com> wrote in message
news:%23W8gfnCPEHA.556@tk2msftngp13.phx.gbl...
> Right, but that won't work for Guid, DateTime or String (to name a few).
> Isn't there a method somewhere in the framework that just returns the
> default value for a given type?
>
> "Nicholas Paldino [.NET/C# MVP]" <mvp@spam.guard.caspershouse.com> wrote
in
> message news:eGQOBlCPEHA.632@TK2MSFTNGP12.phx.gbl...
> > 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: Dave: "Re: Enum data type question?"
- Previous message: Dmitriy Lapshin [C# / .NET MVP]: "Re: Wrapping text in DataGridTextBoxColumn"
- In reply to: John Wood: "Re: default values"
- Next in thread: John Wood: "Re: default values"
- Reply: John Wood: "Re: default values"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|