473,407 Members | 2,326 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,407 software developers and data experts.

Why ValueType is class?

Hello,
Just curious to see why ValueType (which the base for all value types) is a
class. I thought "class" would make a type a reference type. However,
IsValueType for ValueType is returning true.
--Musthafa
Sep 13 '06 #1
6 2010
Manikkoth <Ma*******@discussions.microsoft.comwrote:
Just curious to see why ValueType (which the base for all value types) is a
class.
Because it doesn't make sense for it to be a value type - what would
the values be? Further, value types can't be derived from...
I thought "class" would make a type a reference type. However,
IsValueType for ValueType is returning true.
It shouldn't - ValueType is definitely a reference type. The following
code prints out "False" on my box:

using System;

class Test
{
static void Main()
{
Console.WriteLine (typeof(ValueType).IsValueType);
}
}

Could you give some code showing it returning true?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Sep 13 '06 #2
"Manikkoth" <Ma*******@discussions.microsoft.comwrote in message
news:7B**********************************@microsof t.com...
Hello,
Just curious to see why ValueType (which the base for all value types) is
a
class. I thought "class" would make a type a reference type. However,
IsValueType for ValueType is returning true.
I believe it is a bit of trickery on the part of .net. When you declare an
int you get an int, not an object, but it needs to appear to inherit from
object so that you can assign an int to an object variable.
--Musthafa

Sep 13 '06 #3
Michael C <no****@nospam.comwrote:
"Manikkoth" <Ma*******@discussions.microsoft.comwrote in message
news:7B**********************************@microsof t.com...
Hello,
Just curious to see why ValueType (which the base for all value types) is
a
class. I thought "class" would make a type a reference type. However,
IsValueType for ValueType is returning true.

I believe it is a bit of trickery on the part of .net. When you declare an
int you get an int, not an object, but it needs to appear to inherit from
object so that you can assign an int to an object variable.
And to make things more confusing, there are actually *two* int types
(two types for any value type, indeed) as far as the CLR is concerned -
the value type, and the boxed version of the value type. The value type
itself doesn't inherit from anything, but the boxed type inherits from
ValueType. (It's all a bit odd, frankly.)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Sep 14 '06 #4
"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:MP************************@msnews.microsoft.c om...
And to make things more confusing, there are actually *two* int types
(two types for any value type, indeed) as far as the CLR is concerned -
the value type, and the boxed version of the value type. The value type
itself doesn't inherit from anything, but the boxed type inherits from
ValueType. (It's all a bit odd, frankly.)
Actually what you just said makes it quite clear to me at least. What they
are showing in the object browser is the boxed type and the real valuetype
isn't shown at all.

Michael
Sep 14 '06 #5
It is kinda mind binding, but makes some sense as you said. Allocation of
ValueType occurs on the stack and primitive types have special encoding in
metadata in the system and have explicit opcodes that can operate on them
(i.e. add, etc). ValueTypes can be moved on the gc heap via boxing by making
a copy of the instance. When on the stack, ValueTypes are not self
describing and are tracked by flow analysis. ValueTypes on the heap carry
type information (i.e. MethodTable) and are self describing.

--
William Stacey [MVP]

"Michael C" <no****@nospam.comwrote in message
news:On****************@TK2MSFTNGP03.phx.gbl...
| "Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
| news:MP************************@msnews.microsoft.c om...
| And to make things more confusing, there are actually *two* int types
| (two types for any value type, indeed) as far as the CLR is concerned -
| the value type, and the boxed version of the value type. The value type
| itself doesn't inherit from anything, but the boxed type inherits from
| ValueType. (It's all a bit odd, frankly.)
|
| Actually what you just said makes it quite clear to me at least. What they
| are showing in the object browser is the boxed type and the real valuetype
| isn't shown at all.
|
| Michael
|
|
Sep 14 '06 #6
William Stacey [MVP] wrote:
It is kinda mind binding, but makes some sense as you said. Allocation of
ValueType occurs on the stack and primitive types have special encoding in
metadata in the system and have explicit opcodes that can operate on them
(i.e. add, etc). ValueTypes can be moved on the gc heap via boxing by making
a copy of the instance. When on the stack, ValueTypes are not self
describing and are tracked by flow analysis. ValueTypes on the heap carry
type information (i.e. MethodTable) and are self describing.
Just to clarify, that's for ValueTypes living as independent objects on
the heap.

In unboxed form, value type values can still live on the heap as part
of other objects - the type information is then part of the "owner"
object, but the value itself is not self-describing.

Just trying to avoid the "value types are allocated on the stack,
reference types are allocated on the heap" confusion which sometimes
occurs.

Jon

Sep 14 '06 #7

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

Similar topics

4
by: Shawn B. | last post by:
Greetings, Is it possible to create a custom ValueType object in C#? Or must I use managed C++ for that? Thanks, Shawn
4
by: Brian Brane | last post by:
I have properties that wrap DataRow columns as in: public int aNumber { get{ return m_DataRow; } set{ m_DataRow = value; } } If the column happens to contain DBNull, I get a cast exception...
2
by: Eric Newton | last post by:
Since String.Format has to box all value types to accomodate the params, and just for sheer efficiency, are there possibly any plans for a FormatValue method to minimize boxing? public static...
6
by: Sahil Malik | last post by:
Okay, I can't inherit from System.ValueType. Why this restriction?? What I am trying to acheive is, create my own ValueType called "Money". So if I have a decimal that has value 1.93991, when...
6
by: Aryeh Holzer | last post by:
Let me start with a quote from the C# Programmers Reference (where I learned the cool word "covariance"): "When a delegate method has a return type that is more derived than the delegate...
1
by: INeedADip | last post by:
PropertyInfo props = obj.GetType().GetProperties(); foreach (PropertyInfo p in props) if (p.PropertyType is ValueType) this._commonProperties.Add(p.Name, p.GetValue(request, null).ToString()); ...
3
by: james | last post by:
Hi guys, I create a delegate and pass in a local variable. When the variable is a reference type everything works fine, but when it is a valuetype the delegate uses the value of the last...
7
by: colin | last post by:
Hi, Im a bit confused about when you cast a value type to an object. I have a property grid wich stores the data and the defualt as objects. the data is limited to primitive types, structs and...
2
by: Veeranna Ronad | last post by:
Hello, Our application gets datetime from an interface function. This function returns "ValueType". How to copy datetime content from this "ValueType" to DateTime variable? Thanks in advance...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
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,...
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...
0
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...
0
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,...

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.