473,796 Members | 2,628 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2034
Manikkoth <Ma*******@disc ussions.microso ft.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.WriteLi ne (typeof(ValueTy pe).IsValueType );
}
}

Could you give some code showing it returning true?

--
Jon Skeet - <sk***@pobox.co m>
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*******@disc ussions.microso ft.comwrote in message
news:7B******** *************** ***********@mic rosoft.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*******@disc ussions.microso ft.comwrote in message
news:7B******** *************** ***********@mic rosoft.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.co m>
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.co mwrote in message
news:MP******** *************** *@msnews.micros oft.com...
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******** ********@TK2MSF TNGP03.phx.gbl. ..
| "Jon Skeet [C# MVP]" <sk***@pobox.co mwrote in message
| news:MP******** *************** *@msnews.micros oft.com...
| 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
2828
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
10101
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 since DBNull cannot be converted to int. I wrote the following method that looks up the column's data type and if it is a ValueType, returns the default value for the ValueType.
2
1407
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 string FormatValue(string formatSpec, System.ValueType valueArgs); public static string FormatValue(string formatSpec, System.ValueType arg1) { return FormatValue(formatSpec,new ValueType { arg1 }); }
6
3061
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 cast'ed to Money, it gives me back "1.93", and I can easily write implicit and explicit casts to do a to-and-fro conversion. But I can't do such a conversion to-and-from from object, BECAUSE, my object inherits from System.Object, and I have no...
6
2144
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 signature, it is said to be covariant. Because the method's return type is more specific than the delegate signature's return type, it can be implicitly converted. The method is therefore acceptable for use as a delegate." It then goes on to give an...
1
5953
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()); I get a warning that says p.PropertyType will never be ValueType....and it was right. After stepping through it the types are "System.Boolean" etc... I want to add all integer, string, and boolean types to NameValueCollection,
3
1377
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 instance that was created (sorry that was a mouthful). Heres an example of the problem: private void dlgFavorites_Load(object sender, EventArgs e) { Hashtable bookmarks =
7
1697
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 strings. the structs only have primitive types. however once ive set the data to the defualt, if the data is a struct then any edits change the defualt data as well
2
2322
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 and regards, Veeranna Ronad.
0
9673
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, 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...
0
9524
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,...
0
10449
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10168
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
10003
tracyyun
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...
0
5440
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4114
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
2
3730
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2924
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.