Overview of Unified Type System of C# (C Sharp)Unified Type System means that C# follows a single type system to which all its data types belong. All the data types supported by C# fall under one roof called System.Object. Unlike Java and other programming languages, primitive types like int and byte are subclasses of System.Object .The term Unified Type System can also be called as Common Type System.
Maintaining unified type system has many advantages. Few of them are mentioned below: Assures
seamless integration between objects created in different language, thereby
increasing interoperability Data types belonging to this Unified Type System can be classified into: Value
Types Variables defined using these data types directly hold the
data. Pointer Types are used only during a very specific case, when unsafe code is written. Value Types and Reference Types are the commonly used data types of C#. Given below is a compare and contrast chart on value types and reference types. After reading these differences, no doubt that you wonder how both value types and reference types are part of unified type system using System.Object. But irrespective of these points, value types belong to System.Object. Each primitive type (like int, char) is an object. Consider the following piece of code: public class
sampleClass { Does this look strange? This code is perfectly legal and the output will be 10. This is because the value type variable var1 is an object. Any object will have the following methods (with access modifier as public) by default: Equals(<objectname>):
This function is used to perform equality check. As already known, value
type equality check compares data and reference type equality check is
used to compare if both the references point to the same object. Return
type of this function is Boolean. As per the
discussion till now, value types are objects. Now there arises a question.
Can value types be coded in a similar way as reference types? Yes, you
can. Heres an example: In this example you assign var1 to an object and then you assign this object back to an int variable. This is perfectly legal and you have to cast the value explicitly when converting the object back to primitive type. This code internally uses boxing (converting int to object) and unboxing (converting object to in) concepts. In general, objects have a considerable overhead in terms of performance and memory usage. In C#, value types can be used without any object behavior thereby eliminating its overhead. However if you still prefer to use the object oriented behavior of value type, you can go ahead with it as shown in the earlier example.
|