473,545 Members | 1,890 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Question about value types

When it is being said that, "value types are created on the stack [no
problem with that] or inline [what is being said here?] as part of an
object". If a value type is created in an object, and that object is being
called, the value type in that object, is still created on the stack, I
would say, so I don't understand this inline business. Apart from the fact
that it is my understanding that "inline" as it exists in C++ [e.g. inline
functions] doesn't exist in C#. Could someone please shed some light on this
basic issue for me?
Nov 16 '05 #1
5 2086
Creating instances of value types as local variables creates them on
the stack. If you have a value type as a field of a reference type,
then that is stored as part of the object. It can't be stored on the
stack, as the contents of the stack are valid only till the end of
execution of a method, however reference types outlive that (as they
are created on the heap). The same is the case with an array of value
types, they are stored inline on the heap.

And yes, there is no inline keyword, although the JIT compiler might
inline functions it chooses to.

Regards
Senthil

Nov 16 '05 #2
"sadhu" <se**********@w devs.com> wrote in message
news:11******** **************@ o13g2000cwo.goo glegroups.com.. .

<snipped a couple of lines>
The same is the case with an array of value types, they are stored inline

on the heap.

Would you please explain the meaning of "inline" here?
Nov 16 '05 #3
I think that "inline" is an unfortunate bit of jargon that crept into
the explanation you read.

Think of value types as types that act (and are allocated) in the same
way as an integer or a float.

If you declare an int local variable in a method somewhere, the space
for that int is allocated on the stack. Similarly, if you declare an
int parameter to a method, the argument is _copied_ onto the stack when
the method is called. Value types act in exactly the same way.

However, if you declare an int as a field of a class, space for that
int is reserved within ("inline", as the description you read
unfortunately expressed it) the space allocated on the heap for each
instance of the class. So, each class instance has some space set aside
for it on the heap, and within that space is space for the int. The
same with value types.

Reference types act differently. All reference types are always
allocated on the heap. References (pointers) to those reference types
are then stored in variables, passed to methods, and stored in object
instances according to the same rules as value types. In effect, the
reference (pointer) to the reference type is the value, whereas for
value types the entire bit pattern for the type is the value.

Arrays (that is, native arrays, not ArrayList or other aggregate
structures) act the way you would expect them to given the above
behaviour. An array of ints (or any other value type) is allocated on
the heap as a block of memory big enough to hold all of the values it
must contain. An array of reference types (object instances) is
allocated as an array of references (pointers) to objects that are
stored elsewhere on the heap.

Finally, an array, being a reference type itself, is stored and passed
around as a reference (pointer).

Nov 16 '05 #4
"Bruce Wood" <br*******@cana da.com> wrote in message
news:11******** **************@ g14g2000cwa.goo glegroups.com.. .
<snipped>

I compiled the following text - would you agree with this text?

Variables and constants are storage locations with names. A constant is a
variable with a continuous value. A "read only" variable is a constant.
Variables can be classified in terms of location, size, scope, type, value,
duration (the time span during which they have a life) and how their value
is retrieved. Variables can be located on the heap or on the stack. The size
of the storage location is independent of the compiler or the operating
system, but is determined by their type (e.g. int, double). Scope and
duration are determined by where the variable is declared. Variables
declared in a method - or in the parameter list of a method - are local
variables. They have no life outside the method. Parameters can be straight
values, or additionally qualified as "out" (returns a value), or "ref"
(changes the value). The value of a variable can be numerical or
alpha-numerical (i.e. a string value). Variables like int are called
"intrinsic" , or (=) "built-in" or (=) "primitive" types. Primitive types are
structs. Which means that all variable in C# are objects. The value of a
variable is retrieved directly from the stack ("value types"), or indirectly
via a pointer ("reference types"). Pointers represent values, which
themselves cannot be approached ditectly. Because of this difference value
types are more efficient. Pointers are stored on the stack, whereas the
values they represent are stored on the heap. A variable declared in an
object is stored together (some authors say "inline") with that object. For
example a system array refers to a contiguous block of memory on the heap,
large enough to hold the number and type of elements for which it was
declared. Its pointers are on the stack; its values are on the heap. But in
the case of an array of reference types, there are pointers on the stack -
to pointers on the heap - pointing to values on the heap.
Nov 16 '05 #5
Rats. I wrote a reply on Saturday, but evidently it was swallowed by
the Web.
Variables and constants are storage locations with names. A constant is a variable with a continuous value.
Be careful. In C#, constants often do not have storage locations. They
can be (usually are) just compile-time constructs. For example, the
compiler will render

const int a = 5;
const int b = 6;
int c = a + b;

down to this:

int c = 11;

Variables do have storage locations; constants may not.
A "read only" variable is a constant.
Not exactly. The "readonly" keyword in C# guarantees that the variable
can be changed only in the class's constructor(s) and nowhere else.
However, if the "readonly" variable is a reference to an object on the
heap, the "readonly" keyword does _not_ guarantee that the object
itself cannot be changed outside the constructor, only that the
reference will never point to any other object than the one it was
pointing to when the constructor finished executing.

"readonly" built-in types (such as int and float) are in fact
invariable after the constructor terminates, and are a handy way to
create constants that require some complex calculations... constants
that can't simply be built at compile time.

<snip> The value of a variable can be numerical or alpha-numerical (i.e. a string value).

A little simplistic, I think. From the lowest-level point of view,
variables just contain bit patterns that can be interpreted in various
ways. From a higher-level point of view, variables have an almost
infinite variation of types, as the .NET Framework class library
illustrates. As a trivial example, enumerated types don't fall into
either of these categories, and neither do structs that compose complex
value types.
Variables like int are called "intrinsic" , or (=) "built-in" or (=) "primitive" types. Primitive types are structs.
That's backward, to my way of thinking. Primitive types are not
structs; structs act in many ways like primitive (value) types. The two
have similar behaviour, but that doesn't imply an "is a" relationship,
in my mind, and especially not "primitive type is-a struct".
Which means that all variable in C# are objects.
Now, that's an interesting assertion. It all depends upon how you
define "object". I tend to refer to "objects" in C# as reference types,
but then it's true that in C# value types derive from Object and have
methods, including GetType(), so they have some properties of objects.
However, you can't inherit from them. So are they really full-fledged
"objects"? They certainly more object-like than primitive types in,
say, Java or C++.
The value of a variable is retrieved directly from the stack ("value types"), or indirectly via a pointer ("reference types").
For local variables and method arguments, yes.
Pointers represent values, which themselves cannot be approached directly. Because of this difference value types are more efficient.

Be careful here. Yes, value types are "more efficient" for many
operations, but this does _not_ mean that you should decide between
using a value type and using a reference type based on value types
being "more efficient". This is efficiency at a very low (I'd say even
"trivial") level. Real performance gains are usually had by looking at
efficiency at a far higher level of abstraction than this. In that
sense, the "most efficient" construct--value type or reference
type--for a job is the one that best fits the design. Deciding to use
value types becaues of some idea that they are generally "more
efficient" usually leads to tortured and--yes--inefficient code, in my
experience.
Pointers are stored on the stack, whereas the values they represent are stored on the heap.

Again, yes, for reference types that are local variables or method
arguments.
A variable declared in an object is stored together (some authors say "inline") with that object.

Yes. On the heap.
For example a system array refers to a contiguous block of memory on the heap, large enough to hold the number and type of elements for which it was declared. Its pointers are on the stack; its values are on the heap. But in the case of an array of reference types, there are pointers on the stack - to pointers on the heap - pointing to values on the heap.


Yes, keeping in mind that when you say "its pointers" above, you're
talking about the various references that your application is holding
to the array. Those are held on the stack (or, if they form part of an
object's state, on the heap with the other object state data).

Nov 16 '05 #6

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

Similar topics

19
1738
by: Method Man | last post by:
I understand that arrays and structs can't be passed by value into and out of functions since they can be arbitrarily big. My question is: Why are types allowed to be passed by value? Couldn't my types be arbitraily big as well?
26
1910
by: phoenix | last post by:
Hello, I've got a design question. I need to keep track of some variables and I am planning to put them inside a class or struct. Basically I'm talking about 10 bools, 20 ints and 2 arrays of ints. The size of the arrays would depend on some external value (going from 0 to around 1000 max). I would have an array of max 255 of these...
19
1628
by: Xandau | last post by:
hello all, i wotk with java every day but last time i have interested in C#. everything goes great except one thing... in java everything is a reference (except plain types) so i thought that in C# will be the same - i was wrong.... or if in C# is the same as in java please tell me ;-) I have an ArrayList names ls : // Point is a class...
13
2290
by: gmccallum | last post by:
General Info: A struct is stored on the stack and a class on the heap. A struct is a value type while a class is a reference type. Question: What if a struct contains a string property(variable). The string would be a reference type being contained in a value type. Would this filter up and cause the stack to now be a reference type...
6
1710
by: Mike | last post by:
Lets just say my app is done HOO HOO. Now, I'm accessing the database via a web service and one thing i noticed that my app is running real slow. When I first started working on the app is ran pretty quick returned the data to the screens in about 2 - 3 seconds. Now its going about 5 - 10 seconds. How can I beef it up for better performance.
13
1548
by: cgough | last post by:
My true programming language is C++. I am at best a VB6 hacker that is just getting into VB.NET. I have a quick question about when to new and when not to new. Consider the following 2 classes. In the first I new an integer and assign it to i, in the second one I don't bother. In both cases, an integer is created and I can use it. If I...
20
1880
by: David | last post by:
I feel like an idiot asking this but here goes: I understand the 'concept' of scope and passing data by value and/or by reference but I am confused on some specifics. class example{ int i; //my global var private void method1(int myInt){ int x; //local var etc...
20
4011
by: tshad | last post by:
Using VS 2003, I am trying to take a class that I created to create new variable types to handle nulls and track changes to standard variable types. This is for use with database variables. This tells me if a variable has changed, give me the original and current value, and whether the current value and original value is/was null or not. ...
17
2303
by: Ben Bacarisse | last post by:
candide <toto@free.frwrites: These two statements are very different. The first one is just wrong and I am pretty sure you did not mean to suggest that. There is no object in C that is the same as its address. The second one simply depends on a term that is not well-defined. Most people consider the type to be an important part of...
0
7410
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...
0
7923
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7437
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
5984
agi2029
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...
1
5343
isladogs
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...
0
3466
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1025
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
722
bsmnconsultancy
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...

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.