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

• Guarantees Type Safety

Data types belonging to this Unified Type System can be classified into:

• Value Types – Variables defined using these data types directly hold the data.

• Reference Types – Reference Type Variables hold a reference to the data.

• Pointer Types – Used when you write unsafe code.

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.

Unified Type System C#

Unified Type System in C Sharp

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 {
public static void Main( ) {
int var1 = 10;
Console.WriteLine(var1.ToString());
}
}

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.

• GetHashCode( ): Converts object’s value into unique hash code value. Return type is Int32.

• GetType( ): Metadata associated with the type of your object can be accessed using this method. This method will be useful when you use Reflection API. Return type is Type.

• ToString( ): This method is used to display the fully qualified class name of the object. But in most of the cases it is overridden to cast the actual data into string and display its value. Return type is obviously String.


Other than these public methods, it has one more method finalize( ) that is protected.

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. Here’s an example:
public class sampleClass {
public static void Main( ) {
int var1 = 10;
object objVar1 = var1;
int var2 = (int) objVar1;
Console.WriteLine(“{0}”, var2);
}
}

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.

|Designing applications in .Net using Service Oriented Architecture | How to Create and Use Anonymous Delegates in C# (C Sharp) |How to Define Custom Attributes in C# (C Sharp)| How To Handle Errors While Developing ASP.NET Applications | How to Use Indexer in Classes and Interfaces of C# (C Sharp)| Illustration of Access Keywords (base, this) with Examples in C# (C Sharp) | Illustration of Null Coalesce Operator (??) in C# (C Sharp) |Indexers Vs Properties in C# (C Sharp) | More about Reserved Attributes of C# (C Sharp)| Overview of Unified Type System of C# (C Sharp) | Purpose of Delegates in C# (C Sharp) |How does Simple Object Access Protocol (SOAP) help in Internet Communication |How to Create and Use Anonymous Delegates in C# (C Sharp)|

 


“Amazon and the Amazon logo are trademarks of Amazon.com, Inc. or its affiliates.”

| Privacy Policy for www.dotnet-guide.com | Disclosure | Contact |

Copyright - © 2004 - 2024 - All Rights Reserved.