Even though CSC does its best to detect use of unassigned variables, it
often misses it... for example if you just declare a double in a class
without assigning a default value, it has a default value of 0 and lets you
use it anyway.
My question is, how can I retrieve the default value for a given type? The
CLR obviously has these defaults stored somewhere and I was hoping to get
hold of it.
I hoped the following code would work, but TypeInitializer is null:
double d = 12;
ConstructorInfo con = d.GetType().Typ eInitializer;
Console.WriteLi ne(con.Invoke(n ew object[] { }));
Any ideas? 29 3038
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.
Yes, that may be true -- but, given an arbitrary value type, how do I
programmaticall y 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" <ma************ ********@mvps.o rg> wrote in message
news:e4******** ******@TK2MSFTN GP11.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.
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]
- mv*@spam.guard. caspershouse.co m
"John Wood" <sp**@isannoyin g.com> wrote in message
news:uu******** ******@TK2MSFTN GP09.phx.gbl... Yes, that may be true -- but, given an arbitrary value type, how do I programmaticall y 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" <ma************ ********@mvps.o rg> wrote in message news:e4******** ******@TK2MSFTN GP11.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.
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]" <mv*@spam.guard .caspershouse.c om> wrote in
message news:eG******** *****@TK2MSFTNG P12.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] - mv*@spam.guard. caspershouse.co m
"John Wood" <sp**@isannoyin g.com> wrote in message news:uu******** ******@TK2MSFTN GP09.phx.gbl... Yes, that may be true -- but, given an arbitrary value type, how do I programmaticall y 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" <ma************ ********@mvps.o rg> wrote in message news:e4******** ******@TK2MSFTN GP11.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.
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]
- mv*@spam.guard. caspershouse.co m
"John Wood" <sp**@isannoyin g.com> wrote in message
news:%2******** *******@tk2msft ngp13.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]" <mv*@spam.guard .caspershouse.c om> wrote
in message news:eG******** *****@TK2MSFTNG P12.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] - mv*@spam.guard. caspershouse.co m
"John Wood" <sp**@isannoyin g.com> wrote in message news:uu******** ******@TK2MSFTN GP09.phx.gbl... Yes, that may be true -- but, given an arbitrary value type, how do I programmaticall y 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" <ma************ ********@mvps.o rg> wrote in message news:e4******** ******@TK2MSFTN GP11.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.
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard .caspershouse.c om> wrote in
message news:eG******** *****@TK2MSFTNG P12.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.
The only problem is most value types don't have default constructors(I think
MC++ allowed this for a while, but don't think any other languages did). The
new int() style syntax supported by C# is actually a syntactic sugar for the
initobj instruction, which tells the CLR to zero out a given value type.
Unfortunatly the langage as it stands doesn't support any syntax to
initialize a variable to its default. Tthe C# 2.0 .default operator should
give the behaviour the OP is looking for, if I understand him correctly, but
I havn't taken the time to verify it again in the spec preview so it could
just be an artifact of the current build, but I don't think so. Hope this helps.
-- - Nicholas Paldino [.NET/C# MVP] - mv*@spam.guard. caspershouse.co m
"John Wood" <sp**@isannoyin g.com> wrote in message news:uu******** ******@TK2MSFTN GP09.phx.gbl... Yes, that may be true -- but, given an arbitrary value type, how do I programmaticall y 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" <ma************ ********@mvps.o rg> wrote in message news:e4******** ******@TK2MSFTN GP11.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.
If you run GetConstructor on the value type it returns null though...
doesn't look like any of the value types can provide a default constructor.
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard .caspershouse.c om> wrote in
message news:OZ******** ******@TK2MSFTN GP12.phx.gbl... 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] - mv*@spam.guard. caspershouse.co m
"John Wood" <sp**@isannoyin g.com> wrote in message news:%2******** *******@tk2msft ngp13.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]" <mv*@spam.guard .caspershouse.c om> wrote in message news:eG******** *****@TK2MSFTNG P12.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] - mv*@spam.guard. caspershouse.co m
"John Wood" <sp**@isannoyin g.com> wrote in message news:uu******** ******@TK2MSFTN GP09.phx.gbl... > Yes, that may be true -- but, given an arbitrary value type, how do
I > programmaticall y 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" <ma************ ********@mvps.o rg> wrote in message > news:e4******** ******@TK2MSFTN GP11.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. > >
I suppose this answers my question... although it's disappointing the
initobj instruction isn't exposed through the language.
Thanks.
"Daniel O'Connell [C# MVP]" <onyxkirx@--NOSPAM--comcast.net> wrote in
message news:OK******** ******@TK2MSFTN GP10.phx.gbl... "Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard .caspershouse.c om> wrote
in message news:eG******** *****@TK2MSFTNG P12.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. The only problem is most value types don't have default constructors(I
think MC++ allowed this for a while, but don't think any other languages did).
The new int() style syntax supported by C# is actually a syntactic sugar for
the initobj instruction, which tells the CLR to zero out a given value type. Unfortunatly the langage as it stands doesn't support any syntax to initialize a variable to its default. Tthe C# 2.0 .default operator should give the behaviour the OP is looking for, if I understand him correctly,
but I havn't taken the time to verify it again in the spec preview so it could just be an artifact of the current build, but I don't think so.
Hope this helps.
-- - Nicholas Paldino [.NET/C# MVP] - mv*@spam.guard. caspershouse.co m
"John Wood" <sp**@isannoyin g.com> wrote in message news:uu******** ******@TK2MSFTN GP09.phx.gbl... Yes, that may be true -- but, given an arbitrary value type, how do I programmaticall y 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" <ma************ ********@mvps.o rg> wrote in message news:e4******** ******@TK2MSFTN GP11.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.
John,
You actually don't need to get a constructor, you can just call the
version of CreateInstance that takes just the type, it will create an
instance with the bits zeroed out, like this:
// Get the type.
Type pobjIntType = typeof(int);
// Create an instance.
object pobjInt = Activator.Creat eInstance(pobjI ntType);
Of course, you only run this code if you have a value type, otherwise,
you just return null (for reference types). The full code I would use is:
public static object GetDefaultValue (Type type)
{
// If type is null, throw an exception.
if (type == null)
// Throw an exception.
throw new ArgumentNullExc eption("type");
// Check to see if the type is a value type.
// If it is not, return null.
if (!type.IsValueT ype)
// Return null.
return null;
// Create an instance of the value type.
return Activator.Creat eInstance(type) ;
}
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m
"John Wood" <sp**@isannoyin g.com> wrote in message
news:%2******** ********@TK2MSF TNGP11.phx.gbl. .. If you run GetConstructor on the value type it returns null though... doesn't look like any of the value types can provide a default
constructor. "Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard .caspershouse.c om> wrote
in message news:OZ******** ******@TK2MSFTN GP12.phx.gbl... 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] - mv*@spam.guard. caspershouse.co m
"John Wood" <sp**@isannoyin g.com> wrote in message news:%2******** *******@tk2msft ngp13.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]" <mv*@spam.guard .caspershouse.c om>
wrote in message news:eG******** *****@TK2MSFTNG P12.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] > - mv*@spam.guard. caspershouse.co m > > "John Wood" <sp**@isannoyin g.com> wrote in message > news:uu******** ******@TK2MSFTN GP09.phx.gbl... > > Yes, that may be true -- but, given an arbitrary value type, how
do I > > programmaticall y 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" <ma************ ********@mvps.o rg> wrote in
message > > news:e4******** ******@TK2MSFTN GP11.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. > > > > > >
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: earl |
last post by:
class temp
{
public:
temp();
foo(char, char, char*);
private:
char matrix;
};
temp::foo(char p, char o, char m = matrix )
|
by: Programmer |
last post by:
Hi All
Here is my problem
I'm using a SQLDataAdapter and DataSet
I use the method FillSchema(myDataset, SchemaType.Source)
The problem is that when i Check the default Values of the Dataset
i can see that the DefaultValue of the columns is system.dbnull!!!!
But in SQL Server i have put as default values in a bigint field the '0' and
|
by: Ook |
last post by:
I'm having trouble comprehending what exactly "default construction" is. I
know how to provide a constructor with initial values, so that if I, for
example, in my code do this:
MyClass zoot(1,2);
It would instantiate MyClass, passing 1 and 2 to my constructor which in
turn would do whatever I want it to. Would a default constructor be as
follows:
|
by: Viorel |
last post by:
Adding new row with default values.
In order to insert programmatically a new row into a database table, without
direct "INSERT INTO" SQL statement, I use the well-known DataTable.NewRow,
DataTable.Rows.Add and DataAdapter.Update member functions. Before adding
the new row object to the Rows collection, all of the row's fields that do
not accept NULL must be assigned, otherwise an exception is thrown. However,
I do not want to assign...
|
by: Matt D |
last post by:
I have a bunch of constants used in my web services that the client
also needs to have. Is there a way to declare public properties that
are part of a class or structure as constants or at least give them a
default value through some XML shaping? Thanks.
| |
by: tg.foobar |
last post by:
my setup:
visual studio 2005
sql server 2000
i'm using a dataset (used to be called typed dataset in 2003), where i
use the MSDataSetGenerator to create a class for me based on the scheme
of my SQL server. This schema doesn't include the default values that
are defined in the SQL server. It's setting all the default values to
NULL. is thre an easy way to get around this? for example, i have a
|
by: swengtoo |
last post by:
In his book "More Effective C++", Scott Meyer suggests in "Item 4" to
"Avoid gratuitous default constructors".
To summarize his claims against default constructors for "the right
classes" he states that:
================== START QUOTE =============
"A default constructor is the C++ way of saying you can get something
for nothing. Constructors initialize objects, so default constructors
initialize objects without any information from...
|
by: Joel |
last post by:
Is it true that if we don't specify a default constructor for
our class, then the C# compiler provides us with its own
that zeroes (or assigns default values) to the data members?
I wrote a no-parameter constructor for my class with an
empty function body. I then instantiated an object and tried
printing its values, amazingly the members were already initialized.
But how is this possible if I have not included any code for doing
so. The...
|
by: Zytan |
last post by:
I have a struct constructor to initialize all of my private (or public
readonly) fields. There still exists the default constructor that sets
them all to zero. Is there a way to remove the creation of this
implicit default constructor, to force the creation of a struct via my
constructor only?
Zytan
|
by: Jess |
last post by:
Hello,
I tried several books to find out the details of object
initialization. Unfortunately, I'm still confused by two specific
concepts, namely default-initialization and value-initialization. I
think default-init calls default constructor for class objects and
sets garbage values to PODs. Value-init also calls default
constructor for class objects and sets 0s to POD types. This is what
I've learned from the books (especially...
|
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...
| |
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,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |