17 April 2013

What is type safety in C# .net

When reading about the advantages of using generics, you will come to know you can write type-safe collections and these are the collections which avoid boxing and unboxing.

After reading such statements if you are getting questions on type safety, then you are in the right place to understand about type safety concept.

Let's try to find the answers for the below questions on type safety:

  1. What is type safety in .net?
  2. When does the type safety check happen and who ensures the type safety in .net?
  3. How type safety checks makes developer life easy?

Now let's find the answers for the type safety questions:

What is type safety in .net?

Type safety prevents assigning a type to another type when are not compatible.
public class Employee{}

public class Student{}
In the above example, Employee and Student are two incompatible types. We cannot assign an object of employee class to Student class variable. If you try doing so, you will get an error during the compilation process.

Cannot implicitly convert type 'Program.Employee' to 'Program.Student'.

As this type safety check happens at compile time it's called static type checking.

public class Employee {}
public class Engineer : Employee {}
public class Accountant : Employee {}

public static void Main(string[] args)
  {
   Accountant accountant = new Accountant();
   Engineer engineer = (Engineer)(accountant as Employee);
  }

In the above example, Engineer and Accountant class derives from the same Employee base class. When tried to type cast object of Accountant class to Engineer class variable, it throws System.InvalidCastException at runtime:

Unable to cast object of type 'Accountant' to type 'Engineer'.

Above type checking happens at runtime, hence it is called runtime type checking.

When does the type safety check happens and who ensures the type safety in .net?

As discussed type safety check happens both at Compile time and at runtime. The compiler will do the compile-time type safety check and CLR will do the runtime safety check.

How type safety checks makes developers life easy?

The advantages of type safety are pretty straightforward.

At compile time, we get an error when a type instance is being assigned to an incompatible type; hence preventing an error at runtime. So at compilation time itself, developers come to know such errors and code will be modified to correct the mistake. So developers get more confidence in their code.

Run time type safety ensures, we don't get strange memory exceptions and inconsistent behavior in the application.

You might be interested in my recent blog posts as well:

Events in C# .NET tutorial: What are events, when to use events, etc

Delegates in C# .NET tutorial: Delegates explained in detail

Generics delegates in c#

Dependency Injection explained

2 comments:

  1. It's simple and informative , Thanks.

    ReplyDelete
  2. its good and clear insight of type safety with concise manner.Thank you Ranganatha its very helpful, keep it up like with such concept.

    ReplyDelete