Home All Groups Group Topic Archive Search About

DefaultValueAttribute usage

Author
10 Sep 2006 11:22 PM
Edward Diener
I have a number of questions regarding the DefaultValueAttribute which
is not explained in the rather terse MSDN documentation for it:

1) Should I use it for all properties, even when the property is
initialized to its equivalent 0 value and I do not specifically
initialized in the constructor of my component in which the property
resides ?

2) Is the default value always a constant expression ?

3) Is it used for reference types ? How ? A reference type is not a
constant value.

4) Can it be used for enumerated types ? How ? Is the constructor taking
an Object applied when used for an enumerated type ?

5) Can it be used for the string type, which is a reference type,
passing it a string constant ?

Author
11 Sep 2006 5:34 AM
Mattias Sjögren
Edward,

>1) Should I use it for all properties, even when the property is
>initialized to its equivalent 0 value and I do not specifically
>initialized in the constructor of my component in which the property
>resides ?

Yes


>2) Is the default value always a constant expression ?

Well it has to be if you want to use the DefaultvalueAttribute.


>3) Is it used for reference types ? How ? A reference type is not a
>constant value.

string is a reference type, so yes.


>4) Can it be used for enumerated types ? How ? Is the constructor taking
>an Object applied when used for an enumerated type ?

I believe so.


>5) Can it be used for the string type, which is a reference type,
>passing it a string constant ?

See 3.


Mattias

--
Mattias Sjögren [C# MVP]  mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

AddThis Social Bookmark Button