Can a struct be derived from a class in c#?
If not, Why can primitive data types, such as int, be derived from the class object? Since the data type int is basically a struct type(value type).
Is this just an exception to the rule?
Can a struct be derived from a class in c#?
If not, Why can primitive data types, such as int, be derived from the class object? Since the data type int is basically a struct type(value type).
Is this just an exception to the rule?
Integers and other value types (e.g. bool) are objects, because it allows them to leverage inheritance (i.e. they have access to the common .Equals(), .GetType(), .ToString() functions).
It's a design decision in the .NET framework. Rather than writing separate functions for all the value types under System.ValueType, they use a common code base.
System.ValueType is a class, and so all structs must derive from a class, namely the class System.ValueType. But it is not allowed to derive from another class than System.ValueType. And it is not even allowed to specify your base class (it must be "automatic" System.ValueType). The class System.ValueType derives from System.Object. This is the reason why your struct inherits all the members of System.Object. And the same with int: It is a struct, and it derives from the class System.ValueType.All structs inherit System.ValueType, which in turn inherits Object.
You cannot change that.
When the run-time allocates a storage location for a type, or generates code to operate on one, it checks whether the type derives from System.ValueType but is not System.ValueType itself. Unless the storage location meets those criteria, it will hold a heap object reference, and any code to operate on its members (fields, methods, properties, etc.) will act upon the referenced object. Otherwise, the storage location will hold all the public and private fields of that type (which will be laid out identically in all storage locations of that type), and any code to operate on its members will operate on the storage location itself.
If an attempt is made to store a value type into a storage location of class ValueType, or a storage location which does not derive from ValueType, the system will generate a new heap object of the storage location's type, and then store a reference to that object in the appropriate storage location. Although storage locations of types deriving from System.ValueType, and code to access them, are treated specially by the run-time, heap object instances which inherit from System.ValueType (such as the newly-created one just mentioned) are simply heap objects that happen to derive from System.ValueType, and have inheritance behaviors which are essentially the same as other types, and may thus be passed around by code expecting to deal with heap references.
A statement like Object Foo = New System.Drawing.Point(3,4); actually involves three kinds of things:
The upshot of all this is that while value types may be defined as inheriting from ValueType which inherits from Object, and while heap objects whose types inherit from ValueType do inherit from Object, storage locations of value types do not hold things that inherit from Object.
System.ValueType but is not System.ValueType itself. Of course, there's also System.Enum which is a class deriving from ValueType, and if you say Enum e = DayOfWeek.Thursday; the same kind of boxing takes place.System.Enum used as a type of storage location. I suppose the fact that it works as one explains why one can't use an Enum constraint (since constraining something to Enum would mean it must either be an enumerated type, or System.Enum which behaves nothing like one). I'm impressed in some ways at how well generics were able to work with the "unified type system", but wish value types and enums had separate Type objects for boxed and unboxed forms, so one could declare a parameter of e.g. type "boxed Int32".Enum like that. The Framework doesn't use it much. However, if you say BindingFlags bf = XXX; if (bf.HasFlag(BindingFlags.Static)) { /* ... */}, then the argument is boxed into a System.Enum storage location. That method was new in .NET 4.0.struct can run avoid boxing and run 30 times as fast as a method that uses type System.Enum. By my understanding, even though C# doesn't provide a means of declaring a type constraint of System.Enum, other .net languages allow it. Given that enum.HasFlag is something programmers might use a lot, expecting it to be fast (since it's just doing a bitwise-AND on a couple of integers) I wonder why MS didn't write that extension method in a language where it could run quickly.The class hiearchy works as follows (simplified):
Object -> ValueType -> int Object -> ValueType -> struct
Structs by definition of c# do not allow inheritance.
Here is a nice article describing the role of stucts within the C# language:
http://msdn.microsoft.com/en-us/library/aa288471(v=vs.71).aspx