By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,895 Members | 1,106 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,895 IT Pros & Developers. It's quick & easy.

Variable declaration/definition

P: n/a
I am relatively new in C#, but have several years C++/Java. I am writing
a little C# console app to "test the waters" and learn the language.

If I have a class defined as:

class MyClass
{
MyClass(string s1, int i2, bool b3)
{
}
}
Why can't I declare a variable on the stack somewhere later on in my
code like this:

// .... code above
MyClass mc("Hello",42, true);

Do I always have to alloc from the heap when instantiating objects whose
ctor have parameters?

Jun 27 '08 #1
Share this Question
Share on Google+
6 Replies


P: n/a
"Annonymous Coward" <me@home.comwrote:
Why can't I declare a variable on the stack somewhere later on in my code
like this:
// .... code above
MyClass mc("Hello",42, true);
AIUI, variables of reference types (which includes all class instances) are
always created on the heap.

Eq.
Jun 27 '08 #2

P: n/a
Annonymous Coward <me@home.comwrote:
I am relatively new in C#, but have several years C++/Java. I am writing
a little C# console app to "test the waters" and learn the language.

If I have a class defined as:

class MyClass
{
MyClass(string s1, int i2, bool b3)
{
}
}
Why can't I declare a variable on the stack somewhere later on in my
code like this:

// .... code above
MyClass mc("Hello",42, true);
That's not how you invoke a constructor in C#. You can just declare a
variable, of course:

MyClass mc;

but that won't create an instance.
Do I always have to alloc from the heap when instantiating objects whose
ctor have parameters?
You always have to allocate from the heap whenever you create *any*
objects, regardless of whether there are parameterless constructors. In
that respect C# is identical to Java. Basically C# is closer to Java
than it is to C++ in many ways.

Note, however, that "new" doesn't always mean "allocate from the heap".
For instance, consider:

DateTime dt = new DateTime(2008, 06, 26);

That calls the DateTime constructor, but doesn't allocate anything on
the heap (assuming it's a normal local variable), because DateTime is a
value type. In this respect C# is different to Java, as Java doesn't
have custom value types, just the primitives.

--
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
Jun 27 '08 #3

P: n/a
On Thu, 26 Jun 2008 11:43:04 -0700, Paul E Collins
<fi******************@CL4.orgwrote:
"Annonymous Coward" <me@home.comwrote:
>Why can't I declare a variable on the stack somewhere later on in my
code
like this:
// .... code above
MyClass mc("Hello",42, true);

AIUI, variables of reference types (which includes all class instances)
are
always created on the heap.
Just to clarify: what Paul means is that the _instances_ are created on
the heap. The variable storing the reference to that instance itself may
well be on the stack (in the case of local variables or method arguments).

Pete
Jun 27 '08 #4

P: n/a
Paul E Collins <fi******************@CL4.orgwrote:
"Annonymous Coward" <me@home.comwrote:
Why can't I declare a variable on the stack somewhere later on in my code
like this:
// .... code above
MyClass mc("Hello",42, true);

AIUI, variables of reference types (which includes all class instances) are
always created on the heap.
Nearly. The variables themselves are created wherever their context
says they should be - but the *objects* are always on the heap.

--
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
Jun 27 '08 #5

P: n/a
Jon Skeet [C# MVP] <sk***@pobox.comwrote:
Do I always have to alloc from the heap when instantiating objects whose
ctor have parameters?

You always have to allocate from the heap whenever you create *any*
objects, regardless of whether there are parameterless constructors. In
that respect C# is identical to Java. Basically C# is closer to Java
than it is to C++ in many ways.
What I realised I hadn't made clear after I'd posted is that the
variable itself will still live on the stack if it's a local variable
(which isn't being captured or part of an iterator block). The value of
the variable is a reference, and that reference points to somewhere on
the heap. (This is assuming we're still talking about a reference type
variable, of course.)

--
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
Jun 27 '08 #6

P: n/a
Jon Skeet [C# MVP] wrote:
Annonymous Coward <me@home.comwrote:
>I am relatively new in C#, but have several years C++/Java. I am writing
a little C# console app to "test the waters" and learn the language.

If I have a class defined as:

class MyClass
{
MyClass(string s1, int i2, bool b3)
{
}
}
Why can't I declare a variable on the stack somewhere later on in my
code like this:

// .... code above
MyClass mc("Hello",42, true);

That's not how you invoke a constructor in C#. You can just declare a
variable, of course:

MyClass mc;

but that won't create an instance.
>Do I always have to alloc from the heap when instantiating objects whose
ctor have parameters?

You always have to allocate from the heap whenever you create *any*
objects, regardless of whether there are parameterless constructors. In
that respect C# is identical to Java. Basically C# is closer to Java
than it is to C++ in many ways.

Note, however, that "new" doesn't always mean "allocate from the heap".
For instance, consider:

DateTime dt = new DateTime(2008, 06, 26);

That calls the DateTime constructor, but doesn't allocate anything on
the heap (assuming it's a normal local variable), because DateTime is a
value type. In this respect C# is different to Java, as Java doesn't
have custom value types, just the primitives.
Thanks Jon - its very clear to me now. tx
Jun 27 '08 #7

This discussion thread is closed

Replies have been disabled for this discussion.