System.ValueType Understanding - c#

I tried to create a ValueType.
I understand that creating a struct would help me.
I also tried to derive a type from System.ValueType which is an abstract class.
But I got a compiler error message
".. cannot derive from special class System.ValueType"
When I see the metadata of ValueType, it looks to be a regular abstract class. Any non sealed class should be derivable. But System.ValueType is not a sealed class.
What made it special?
Is it the C# compiler that senses it as special?
If so, is it recommended as a rule for compiler design? I mean is it part of Common Language Specification?

ValueType is a little white lie.
The built-in numeric types (int, long, byte), char, enums and structs are all value types.
This means that they have a different concepts of identity and equivalence to object types. If I do x = y and x and y are reference types, then x and y now point to precisely the same object. However, if I do x = y and x and y are value types, then x and y are now two completely different objects that happen to be identical. (This is also reflected in == and Equals, though that can be overridden).
(This is where people get sidetracked by talking about the stack and the heap, if they haven't already, really that's an implementation detail and while important, is not the point of the distinction between value and reference types).
Now, mostly this is all and good but one thing about reference types is that they all benefit from inheriting from System.Object. The value type int doesn't really, and that's good too as it's much better in many ways that it just be four bytes of memory handled by the lovely CPU instructions that are so good at doing so. Still, it's sometimes useful to be able to treat an int as if it also inherited from System.Object, and so you can.
Of course, this means that you may do things with int that only make sense to do on a System.Object, so when you do so the int is "boxed" and can then be "unboxed" again.
This is great, but what if we wanted to do something specific to value types? More to the point, what if the designers of the CLR did (in particular, they wanted a GetHashCode for value types that related to the value-based equivalence descibed above, rather than the identity-based equivalence that objects have)?
For this purpose we have ValueType. The system treats all value types as inheriting from this class, which in turn inherits from Object. Enum in turn inherits from value type and all enum types inherit from it, allowing some common functionality across all enums.
So, if you ever want to treat a superclass of all value types, use ValueType, but if you want to actually create a value type, create a struct or an enum as appropriate.
The Common Type System explanation:
A structure is a value type that derives implicitly from System.ValueType, which in turn is derived from System.Object. A structure is very useful for representing values whose memory requirements are small, and for passing values as by-value parameters to methods that have strongly typed parameters. In the .NET Framework class library, all primitive data types (Boolean, Byte, Char, DateTime, Decimal, Double, Int16, Int32, Int64, SByte, Single, UInt16, UInt32, and UInt64) are defined as structures.
Like classes, structures define both data (the fields of the structure) and the operations that can be performed on that data (the methods of the structure). This means that you can call methods on structures, including the virtual methods defined on the System.Object and System.ValueType classes, and any methods defined on the value type itself. In other words, structures can have fields, properties, and events, as well as static and nonstatic methods. You can create instances of structures, pass them as parameters, store them as local variables, or store them in a field of another value type or reference type. Structures can also implement interfaces.
Value types also differ from classes in several respects. First, although they implicitly inherit from System.ValueType, they cannot directly inherit from any type. Similarly, all value types are sealed, which means that no other type can be derived from them. They also do not require constructors.
For each value type, the common language runtime supplies a corresponding boxed type, which is a class that has the same state and behavior as the value type. An instance of a value type is boxed when it is passed to a method that accepts a parameter of type System.Object. It is unboxed (that is, converted from an instance of a class back to an instance of a value type) when control returns from a method call that accepts a value type as a by-reference parameter. Some languages require that you use special syntax when the boxed type is required; others automatically use the boxed type when it is needed. When you define a value type, you are defining both the boxed and the unboxed type.
The strangeness of ValueType is to allow the above to happen.

Structures are value types. Value types are special because they are allocated on the stack rather than the heap. To "inherit" from ValueType, you must create a struct.

Not being able to derive from ValueType is specific to the C# compiler. If we look at managed C++ code:
value class Foo {};
value class Foo : System::ValueType {};
Both of these compile and are identical. Of course,
ref class Foo : System::ValueType {};
Will give error C3050: a ref class cannot inherit from 'System::ValueType'.
Not sure what other compilers allow.
If you want to derive from ValueType in C#, use struct and not class, and the compiler takes care of it.

C# does not allow value types to inherit from other classes. Value types (struct) can implement interfaces.

you cannot subclass ValueType directly. All value types derives from ValueType implicitly. Unlike with reference types, you cannot derive a new type from a value type. However, like reference types, structs can implement interfaces.
see MSDN to know more

A 'class' extends 'System.Object', a 'struct' extends 'System.ValueType'. If you could make a class extend 'System.ValueType', then there wouldn't be much point to the 'struct' keyword.
Yes
Yes. There should only be one way to accomplish any given thing. As soon as two methods of doing something are provided, you've just make everything more complicated. General rule.

This is what perplexes me... Enum is derived from ValueType. Which makes me think the above is just a compiler restriction.
[Serializable, ComVisible(true)]
public abstract class Enum : ValueType, IComparable, IFormattable, IConvertible
{
...
}

Related

C# Struct, Handling Bool type with methods?

I thought that I knew how to handle structures, since I have programmed in C for years. However, I have come across this struct definition in a C# program that I am attempting to understand. It is populated with booleans and each instance of the struct is going to be a cell in an array (not shown here). I expect that the override in line 3 is used to override a method "ToString()" in a base class.
public struct Cell
{
public bool occupied;
public Cell(bool occupied) { this.occupied = occupied; }
public override string ToString() { return occupied ? "x" : "."; }
}
I understand the first line above. I believe that I am confused about the use of methods in structures, as I am assuming that the second and third lines in the above struct definition are methods. The second line is very confusing to me.
Thank You
Tom
The second line in the struct is the constructor of the struct (so yeah, it's basically a method) which takes a boolean as a parameter and assigns the value passed in to the "occupied" field.
The third line is an override of the ToString method, which is inherited by everything because it's a built-in method of the Object class, which is a superclass of every other object that exists in C#. By default, it simply outputs the fully-qualified class/struct name.
The struct of C# has little to do with the struct from C. In .NET, all (for practical purposes) entities inherit from Object.
It does not matter if they are classes (reference types) or structs (value types); both can have methods, constructors, properties, attributes etc.. The only limitation is that you cannot extend a concrete value type (that is, you cannot inherit from a struct), since their memory footprint and type is predefined when unboxed. Therefore, you can think of all value types being "final".
Also, you can have constructors on structs (which is what you're seeing in the middle of your example code). Note, however, that a struct always also has an implicit "default constructor" with no arguments, which initializes the data to all binary 0.
What exactly is your confusion? You have two good guesses about lines #2 and #3, which can be easily verified with a simple test case.
Yes, the second line is a constructor, which receives a boolean and initializes a field's value.
The third line, as you guessed, is also a method which overrides the base ToString. In this case, since there's no explicit base class, the type extends the methods found in System.Object, known colloquially in C# as object. object's implementation would simply print out the type name ("MyNamespace.Cell"), but this implementation overrides it with the contents of the boolean field.
Structs and classes are very similar to use in C#. So your struct can have methods and a constructor. But there are a few differences. For example: A struct is called by value, a class by reference.
To choose the right one of these options look here:
https://msdn.microsoft.com/en-us/library/ms229017%28v=vs.110%29.aspx
Here the differences are explained:
Structs versus classes
Main difference between struct and class in c# is that class instances are reference types, while struct instances are value types (stored in the stack, rather than the heap).
Second line in your code is just a simple constructor. Structs may have constructor as long as they are not empty constructors. (See https://msdn.microsoft.com/en-us/library/aa288208(v=vs.71).aspx)
Third line is overriding the base Object class method ToString().
Structs may define methods, there's nothing wrong with it.
For additional information about structs, make sure to check out MSDN docs about structs

Is a struct just a class that derives from ValueType?

Is it true to say that a struct is just a class which inherits from System.ValueType?
Is the keyword "struct" just syntactic sugar for writing a class with : System.ValueType after the name?
If it is just a class, then is it true to say that not all classes are reference types since structs are technically classes?
Not quite "just syntactical sugar". From the MSDN:
Although ValueType is the implicit base class for value types, you
cannot create a class that inherits from ValueType directly. Instead,
individual compilers provide a language keyword or construct (such as
struct in C# and Structure…End Structure in Visual Basic) to support
the creation of value types.
So could you say that a struct is just a class that inherits from System.ValueType semantically? That'd debatable. All struct are derived from System.ValueType, but you can't explicitly create a class that derives from System.ValueType.
In addition, of course, just being derived from System.ValueType, struct has a lot of differences from class as you probably know. But if not, I have a blog post on some of the key differences here, including but not limited to:
Value types are, of course, passed and assigned by value, not by reference.
A struct cannot accept initialization values for their fields in the definition (they always given values for their declared field types).
A struct can have events, but since they are value types must take care that you aren't subscribing to a copy!
You cannot inherit from a struct.
You cannot create struct parameterless constructor, struct provides one which cannot be overridden.
Creating an overloaded struct constructor does not hide the parameterless constructor.
The this keyword, used in a struct is a value variable, not a reference.
You do not need to use new to create an instance of a struct (but if you do this you must provide a value for all fields before it is used.
MSDN also has some good advice on when to use struct vs class. Because they are value types, you should think of them as such and limit them to smaller things (16 bytes or less) and preferably as immutable representations of a single value (like DateTime, TimeStamp etc.).
Class and struct differences
Structs differ from classes in several important ways:
Structs are value types (Section 11.3.1).
All struct types implicitly inherit from the class System.ValueType (Section 11.3.2).
Assignment to a variable of a struct type creates a copy of the value being assigned (Section 11.3.3).
The default value of a struct is the value produced by setting all value type fields to their default value and all reference type fields to null (Section 11.3.4).
Boxing and unboxing operations are used to convert between a struct type and object (Section 11.3.5).
The meaning of this is different for structs (Section 11.3.6).
Instance field declarations for a struct are not permitted to include variable initializers (Section 11.3.7).
A struct is not permitted to declare a parameterless instance constructor (Section 11.3.8).
A struct is not permitted to declare a destructor (Section 11.3.9).
See also: Choosing Between Classes and Structures

Do interface variables have value-type or reference-type semantics?

Do interface variables have value-type or reference-type semantics?
Interfaces are implemented by types, and those types are either value types or reference types. Obviously, both int and string implement IComparable, and int is a value type, and string is a reference type. But what about this:
IComparable x = 42;
IComparable y = "Hello, World!";
(The question I was trying to answer was presumably deleted because it asked whether interfaces are stored on the stack or the heap, and, as we should all be aware, it's more constructive to think of differences between value and reference types in terms of their semantics rather than their implementation. For a discussion, see Eric Lippert's The stack is an implementation detail.)
Usually, as per the existing answers, it is a reference-type and requires boxing; there is an exception though (isn't there always?). In a generic method with a where constraint, it can be both:
void Foo<T>(T obj) where T : ISomeInterface {
obj.SomeMethod();
}
This is a constrained operation, and is not boxed even if it is a value-type. This is achieved via constrained. Instead, the JIT performs the operation as virtual-call for reference-types, and static-call for value-types. No boxing.
This is about understanding boxing and unboxing of types. In your example, the int is boxed upon assignment and a reference to that "box" or object is what is assigned to x. The value type int is defined as a struct which implements IComparable. However, once you use that interface reference to refer to the value type int, it will be boxed and placed on the heap. That's how it works in practice. The fact that using an interface reference causes boxing to occur by definition makes this reference type semantics.
MSDN: Boxing and Unboxing
A variable or field whose type is IComparable is a reference-type variable or field, regardless of the type of the value assigned to that field. This means that x in the sample code is boxed.
A simple test will demonstrate that. The test is based on the fact that you can only unbox a value type to its original type (and the nullable version of that type):
[TestMethod, ExpectedException(typeof(InvalidCastException))]
public void BoxingTest()
{
IComparable i = 42;
byte b = (byte)i; //exception: not allowed to unbox an int to any type other than int
Assert.AreEqual(42, b);
Assert.Fail();
}
EDIT
On top of that, the C# specification specifically defines reference-type as comprising class types, interface types, array types and delegate types.
EDIT 2
As Marc Gravell points out in his answer, a generic type with an interface constraint is a different case. This doesn't cause boxing.
Variables of interface type will have always have either immutable semantics, mutable reference semantics, or "oddball" semantics (something other than normal reference or value semantics). If variable1 and variable2 are both declared as the same interface type, one performs variable2 = variable1, and one never again writes to either variable, the instance referred to by variable1 will always be indistinguishable from the one referred to be variable2 (since it will be the same instance).
Generic types with interface constraints may have immutable semantics, mutable reference semantics, or "quirky" semantics, but may also have mutable value semantics. This can be dangerous if the interface is not documented as having mutable value semantics. Unfortunately, there is no way to constrain an interface to have either immutable semantics or mutable value semantics (meaning that following variable2 = variable1, it should not be possible to change variable1 by writing variable2, nor vice versa). One could add a "struct" constraint along with the interface constraint, but that would exclude classes which have immutable semantics while not excluding structs that have reference semantics.

Why does every class in .NET inherit from Object?

I don't understand it...
Why do they need a common base?
The question presupposes a falsehood. They don't need a common base type. This choice was not made out of necessity. It was made out of a desire to provide the best value for the customer.
When designing a type system, or anything else for that matter, sometimes you reach decision points -- you have to decide either X or not-X. Common base type or no common base type. When that happens you weigh up the costs and the benefits of X to determine the net value, and then you weigh up the costs and the benefits of not-X to determine the net value, and you go with the one that was higher value. The benefits of having a common base type outweigh the costs, and the net benefit thereby accrued is larger than the net benefit of having no common base type. So we choose to have a common base type.
That's a pretty vague answer. If you want a more specific answer, try asking a more specific question.
I think, mainly in order to have a type to refer to any object. An argument of type object could be a primitive type, a struct, a reference type, just anything. It is important to have such a type.
Then there are some common members that are implemented by every type, like Equals, ToString, GetHashCode. This could indeed be a common interface as well, but then you wouldn't inherit a default implementation.
Because its in the spec:
8.9.9 Object type inheritance
With the
sole exception of System.Object, which
does not inherit from any other object
type, all object types shall either
explicitly or implicitly declare
support for (i.e., inherit from)
exactly one other object type. The
graph of the inherits-relation shall
form a singly rooted tree with
System.Object at the base; i.e., all
object types eventually inherit from
the type System.Object.
There are several benefits to this design approach.
Some reasons why many / most are:
To provide common members such as Equals, Finalize, GetHashCode, ToString....
To help with boxing....
Java and C# took a different approach to this. It's mainly to do with performance.
If you imagine that every object can be nullable, then it has to be a pointer to a value, which can be changed to a pointer to null. Now this means that for every object you need at least a pointer and a chunk of memory to store the value.
Java has the concept of a primitive value, which is NOT an object. It doesn't have a pointer and it isn't nullable. This breaks the OOP paradigm but performance wise makes sense.
C# (or more correctly the CLR + BCL) attempted a good compromise, the ReferenceType and ValueType derivations. Anything that derives from ValueType are treated like primitives to the CLR, avoiding having an object reference. However this value can still be treated like an object via boxing, allowing you to have the performance benefits of primitive types but allowing everything to be treated like an object.
The real key difference between these things is the semantics of passing parameters to methods. If everything is an object, then you are passing references to the object, i.e the object can be changed by passing it to a method. Primitives and C# value types are passed by value, so they are effectively copied into the method call and the original value is unchanged.
It's the standard story of development. Try and get it right first, then see if you can optimise it later once you see the bottlenecks. Having pass by value semantics also allow you to prevent coding mistakes from mutability. (eg. passing a class vs a struct in C#)
ToString. For example.
Useful for Boxing and Unboxing
see reference
Every object in .NET shares common properties and methods. However, these are then divided into two categories: value types and reference types. Value types (ie, int) are stored on the stack, Reference types (ie, your custom class) are stored on the heap. Reference types store a reference to the actual data (thats on the heap). Value types directly contain their data.
You can read more over at MSDN:
http://msdn.microsoft.com/en-us/library/system.object.aspx
As a side note to other "answers" a struct is a value type, that also inherits from object.
Maybe the answer is that its assumed that we are programming object-oriented style/paradigm? A ball is an object. A sword is an object. An employee is an object. A purchase order is an object.
Some companies design their .NET (or Java or Ruby or PHP) applications to inherit from a common base class, so that they can all be treated the same way in their system. If I remember correctly from back in my old Java days... all EJBs share the same base class so that they can be managed and identified uniformly.

How to define generic type limit to primitive types?

I have the following method with generic type:
T GetValue<T>();
I would like to limit T to primitive types such as int, string, float but not class type. I know I can define generic for class type like this:
C GetObject<C>() where C: class;
I am not sure if it is possible for primitive types and how if so.
You can use this to limit it to value types:
where C: struct
You also mention string. Unfortunately, strings won't be allowed as they are not value types.
Actually this does the job to certain extend:
public T Object<T>() where T :
struct, IComparable, IFormattable, IConvertible, IComparable<T>, IEquatable<T>
To limit to numeric types you can get some useful hints of the following samples defined for the ValueType class
Here's what you're looking for:
T GetObject<T>() where T : struct;
There is no generic constraint that matches that set of things cleanly. What is it that you actually want to do? For example, you can hack around it with runtime checks, such as a static ctor (for generic types - not so easy for generic methods)...
However; most times I see this, it is because people want one of:
to be able to check items for equality: in which case use EqualityComparer<T>.Default
to be able to compare/sort items: in which case use Comparer<T>.Default
to be able to perform arithmetic: in which case use MiscUtil's support for generic operators
What are you actually trying to do in the method? It could be that you actually need C to implement IComparable, or someother interface. In which case you want something like
T GetObject<T> where T: IComparable
I'm under the same need, I want to create a method that should retrieve a List where T should be a primitive type like int, double, decimal, etc...
Based on this Microsoft documentation:
https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/unmanaged-types
Looks like the right approach is to use
where T : unmanaged
quoting:
A type is an unmanaged type if it's any of the following types:
sbyte, byte, short, ushort, int, uint, long, ulong, char, float,
double, decimal, or bool Any enum type Any pointer type Any
user-defined struct type that contains fields of unmanaged types only
and, in C# 7.3 and earlier, is not a constructed type (a type that
includes at least one type argument)
Also important quote:
Beginning with C# 7.3, you can use the unmanaged constraint to specify
that a type parameter is a non-pointer, non-nullable unmanaged type.
Beginning with C# 8.0, a constructed struct type that contains fields
of unmanaged types only is also unmanaged...
If you need types for which you can use languages features reserved for managed types such as the sizeof operator, use "unmanaged".
where C: unmanaged

Categories