473,387 Members | 1,742 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Value type and a parameterless constructor


"The C# specification states that all value types have a default
parameterless constructor, and it uses the same syntax to call both
explicitly declared constructors and the parameterless one, relying on
the compiler to do the right thing underneath."

Does it mean that if we create a Value type with other constructors,
we will still have a parameterless constructor provided by compiler?
Nov 3 '08 #1
9 2673
For value types you can't explicitly create a parameterless constructor,
because the compiler doest that.

Yes, you will still have a parameterless constructor provided by the
compiler.
puzzlecracker wrote:
"The C# specification states that all value types have a default
parameterless constructor, and it uses the same syntax to call both
explicitly declared constructors and the parameterless one, relying on
the compiler to do the right thing underneath."

Does it mean that if we create a Value type with other constructors,
we will still have a parameterless constructor provided by compiler?
Nov 3 '08 #2
On Mon, 03 Nov 2008 10:15:25 -0800, puzzlecracker <ir*********@gmail.com>
wrote:
"The C# specification states that all value types have a default
parameterless constructor, and it uses the same syntax to call both
explicitly declared constructors and the parameterless one, relying on
the compiler to do the right thing underneath."

Does it mean that if we create a Value type with other constructors,
we will still have a parameterless constructor provided by compiler?
Well, when you created a value type with some other constructor, did it
also wind up with a parameterless constructor?

This is trivial to test yourself, so you should just do that.
Nov 3 '08 #3
On Nov 3, 1:55*pm, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:
On Mon, 03 Nov 2008 10:15:25 -0800, puzzlecracker <ironsel2...@gmail.com>*
wrote:
"The C# specification states that all value types have a default
parameterless constructor, and it uses the same syntax to call both
explicitly declared constructors and the parameterless one, relying on
the compiler to do the right thing underneath."
Does it mean that if we create a Value type with other constructors,
we will still have a parameterless constructor provided by compiler?

Well, when you created a value type with some other constructor, did it *
also wind up with a parameterless constructor?

This is trivial to test yourself, so you should just do that.
Ah, I did write a rudimentary test after i posted the question... I
was more interested as to why parameterless ctor is included,
reasoning behind that is -- that my compiler couldn't explain :)

Thanks
Nov 3 '08 #4
On Mon, 03 Nov 2008 10:57:53 -0800, puzzlecracker <ir*********@gmail.com>
wrote:
Ah, I did write a rudimentary test after i posted the question... I
was more interested as to why parameterless ctor is included,
reasoning behind that is -- that my compiler couldn't explain :)
Okay, well...that's a different question. The answer to that is that
value types, due to the way they can be instantiated, have to always have
a default constructor. In particular, when they are members of other
types, or you make an array of them, the run-time always provides the
default value for the type. There has to be a default constructor for
this to be semantically meaningful.

It's actually a bit of cheat, because the default constructor isn't really
called in those scenarios. If you look at the definition of the default
values for value types, you'll see that they are all in some way "zero".
So what really happens is that the run-time allocates memory that's been
zero-initialized. But by defining value type operations in a way that
ensures that they will always be initialized to zero, this allows that
easy implementation to meet the specification.

Pete
Nov 3 '08 #5
Each constructor for a value type *MUST *initialize all the fields or it
must call some other constructor which does initialize all other fields.

eg: This code doesn't compile
struct Test
{
int a;
int b;

Test(int x)
{
a = x;
}

}

However this code compiles fine

struct Test
{
int a;
int b;
Test(char c)
{
a = b = -1;
}
Test(int x) : this('c')
{
a = x;
}
}

Peter Duniho wrote:
On Mon, 03 Nov 2008 10:15:25 -0800, puzzlecracker
<ir*********@gmail.comwrote:
>"The C# specification states that all value types have a default
parameterless constructor, and it uses the same syntax to call both
explicitly declared constructors and the parameterless one, relying on
the compiler to do the right thing underneath."

Does it mean that if we create a Value type with other constructors,
we will still have a parameterless constructor provided by compiler?

Well, when you created a value type with some other constructor, did
it also wind up with a parameterless constructor?

This is trivial to test yourself, so you should just do that.
Nov 3 '08 #6
On Mon, 03 Nov 2008 11:34:55 -0800, Ashutosh Bhawasinka
<di********@ashutosh.inwrote:
[HTML post snipped]
Please stop posting in HTML. It's considered an abuse of the Usenet
messaging system.
Nov 3 '08 #7
Peter Duniho wrote:
On Mon, 03 Nov 2008 11:34:55 -0800, Ashutosh Bhawasinka
<di********@ashutosh.inwrote:
>[HTML post snipped]

Please stop posting in HTML. It's considered an abuse of the Usenet
messaging system.
I can't say I really mind it, because his client is doing the right thing
and posting both HTML and plain text attachments as multipart/alternative.
If your client is displaying the HTML attachment and you don't like that, it
should have an option for picking your desired format. Clients that don't
understand MIME at all might have a problem, but those should be rare to
nonexistent by now.

I'm actually more annoyed by the top-posting...

--
J.
Nov 3 '08 #8
Ashutosh Bhawasinka <di********@ashutosh.inwrote:
For value types you can't explicitly create a parameterless constructor,
because the compiler doest that.

Yes, you will still have a parameterless constructor provided by the
compiler.
In fact, for value types the compiler doesn't create a constructor in
the IL. This is an interesting impedance mismatch between the CLI spec
and the C# spec - as far as C# is concerned, *all* value types have a
parameterless constructor. As far as the CLI spec is concerned, *no*
value types have a parameterless constructor (you can't define one).
That's why if you fetch the constructors via reflection, you won't see
the parameterless one. However, all value types allow you to create an
instance without specifying parameters - it just uses a different op
code, meaning "wipe the data to 0" basically.

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Nov 4 '08 #9
Peter Duniho <Np*********@nnowslpianmk.comwrote:
It's actually a bit of cheat, because the default constructor isn't really
called in those scenarios. If you look at the definition of the default
values for value types, you'll see that they are all in some way "zero".
So what really happens is that the run-time allocates memory that's been
zero-initialized. But by defining value type operations in a way that
ensures that they will always be initialized to zero, this allows that
easy implementation to meet the specification.
It's even more interesting than that, in terms of the allocation. I
looked into this for a recent StackOverflow question. (Apologies to all
for my woeful neglect of this newsgroup. StackOverflow is proving
somewhat addictive.) Anyway:

http://stackoverflow.com/questions/2...-vs-class-in-c

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Nov 4 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: ahaupt | last post by:
Hi all, I'm implementing the Clone() method through the ICloneable interface and don't quite know how deep I need to go for a deep copy. Example: class A: ICloneable { object _val;
3
by: Paul McKenna | last post by:
Hello I have a custom user control which I have developed. This user control needs to have one property value passed to it from the parent form. I tried using the constructor to do this, the...
9
by: Peter Oliphant | last post by:
I've been told that value structs can have default constructors (I'm using VS C++.NET 2005 PRO using clr:/pure syntax). But the following code generates the following error: value struct...
12
by: Edward Diener | last post by:
Given value class X { public: // Not allowed: X():i(100000),s(10000) { } // Allowed void InitializeDefaults() { i = 100000; s = 10000; } private: int i;
1
by: Nathan Sokalski | last post by:
I have created a custom control for ASP.NET using VB.NET. My control inherits from the System.Web.UI.WebControls.CompositeControl class, and is working fine. However, the Visual Studio .NET designer...
42
by: blisspikle | last post by:
I tried closely copying some code that I found on this group for assigning a type at runtime, but I cannot get it to work. Can someone see what is wrong with my logic? Thanks, Private Sub...
4
by: Macneed | last post by:
i am a newbie, i remember i read a book talking about when u declare a array variable using float ABC = new float; the whole array element in ABC ( ABC to ABC ) will automatic initialize to 0...
14
by: GeezerButler | last post by:
For any given type i want to know its default value. There is a neat keyword called default for doing this like object x = default(DateTime); but I have an instance of Type (called someType) and...
2
by: Hans Kesting | last post by:
on 23-9-2008, Julia B supposed : Usually that errormessage means that you derived a class from a baseclass that has no parameterless constructors. Always when you call a constructor, a...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.