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 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
"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
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
"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
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
|
|
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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
|
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.
|
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 });
}
|
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...
|
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...
| |
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,
|
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 =
|
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
|
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.
|
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...
|
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,...
| |
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...
|
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,...
|
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...
|
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...
|
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |