A Basic Introduction of System Types for Beginners – Part 1


System types plays an important role in building .NET framework application either Application is Windows or Web or Integer data returned by a web services method. All are using .Net framework system type. It’s impossible to create any .Net Framework application without using System types. So we can understand its necessity in .Net Framework.
In addition to System types provided by .Net Framework, we can also create custom (user defined) system types like classes and structures and Interfaces to mention or defined functionality.

In .Net Framework, all functionality are encapsulated into objects, where objects are instances of system type classes. However .Net framework composes itself many predefined system types which is known as base system types. These base types enable you to create application easily and rapidly because of its encapsulating nature of common tasks. Base System types represent a set of predefined data types. Based on the system types in a development environment, you can classify an environment as loosely typed or strongly typed. A loosely typed development environment provides you the flexibility to store different types of data inside a variable. A strongly typed environment ensures that all the rules applicable to the given system type are adhered to.

System Types:

From wikibooks – Types are a way to classify the concepts or objects of a language. The way this classification is organized is called a ‘type system’. The types themselves can also be categorized in different ways by the type system. The first way to categorize types in .NET is to make a difference between types that are part of the framework class libraries (System types) and types that will be constructed by the developer (custom types).
There are many ways to categorize types in .NET Framework. One of them is by the way the objects created based on those types are mapped to the computer memory. This will give us Value types and Reference types.

We all know software development environment are classified on the basis of how the compilers manage data types. The two types of well-known compiler are

  • strongly typed environment and
  • loosely typed environment.

Lets take a look of basic info for Strongly and loosely typed environment.

What is Loosely typed environment –

  • Cannot enforce type safety.
  • Cannot guarantee that there is one and only one particular type of data in a location memory or a variable in given time.
  • Don’t use memory very efficiently because a variable accommodates any possible types of value.

What is strongly typed environment –

  • Can ensure data type safety.
  • Can give guarantee that there is one and only one particular type of data in a location or a variable in given time.
  • Use memory very efficiently.

What is Common Type System

The .Net Framework provides multiple language support using the features known as Common Type System that is built into the CLR(Common Language RunTime) The CTS support a variety and operation found in most programming language and therefore calling one language from another does not require type conversions.Although C# is specially designed for the .Net platform, we can built .Net programs in a number of other languages including C++ and Visual Basic.

    In short, .Net Framework provide common set of data types called Common Type System.

Programing language that support CTS can easily communicate with each other.
All System types in CTS derive from the same base type, which is System.Object. Both categories of System in the .Net Framework, value types and reference type display common intrinsic behavior because they derive from System.Object.
For more reference, have a read
Common Type System-[Wikipedia]
Common Type System-[MSDN]

Working with Special System Types

As we discussed and learned about base system types which enable us to develop robust .Net Framework application easily and rapidly. The .Net Framework encapsulated some special types that take .Net Framework application further with better type safety, fewer bugs and better customizability. These special system types are generics, Nullable types, exception classes and attributes.
Generic is fairly and highly useful concept of .Net Framework. In earlier version of .Net framework, objects are added to collection after implicit casting to the System.Object type. This resulting for compromising type safety. In the .Net Framework you can see the predefined collections and Interfaces that generic provide to create collections objects or define custom types that are type-safe. Whereas Nullable which is also a special System Type used to declare value types that can store null values. Now we cannot forget about other system types which exception classes and attributed. We can use exception classes to handle error conditions during run time whereas we can use attributed to convey information to the rim time about the behavior or programmatic elements, such as classes, enumerators and assemblies.

Generics :

What are Generics :-

  • Generics are the most powerful and prominent feature of C#
  • Generics allow you to define type-safe data structures, without committing to actual data types.
  • The System.Collections.Generics namespace contains the generics collections.
  • The most common use of generics is to create collection classes.
  • Generic classes may be constrained to enable access to methods on particular data types.
  • Generics can act like any data type means there is no need to write any internal code
  • Generics can be refer to a method ,class, structure or an interface in a type safe manner
  • We can create your own generic interfaces, classes, methods, events and delegates.

Implementation of generics :-

The code examples defines a generic class called CommmonData, which is used to create two object. one to store string value and other to store int value. The CommonData class ensures type safety by accepting the required type in its constructor. This implies that value specified for an object of the CommonData class is of same type as that specified while creating the corresponding object.

[sourcecode language=”csharp”] class Program
static void Main(string[] args)
// Declare a list of type string
CommonData name = new CommonData();
name.Value = "Cricket Team";

// Declare a list of type int
CommonData member = new CommonData();
member.Value = 15;
Console.WriteLine("Team Name :" + name.Value);
Console.WriteLine("Number of Member : "+ member.Value);

// Declare the generic class
public class CommonData
private T data;
public T Value
get { return this.data; }
set { this.data = value; }

What are Advantage of generics :-

Reusability : A single generic type definition can be used for multiple scenarios in the same code, without any alterations.
Type Safety : Generic data type provide better type safety, especially in situations where collections are used.
Performance : Generic types perform better than normal system types because they reduce the need boxing, unboxing and type casting the variable or objects.

for more information, have a read
An Introduction to Generics

Coming Next 

In the next article, I will come back with more detail and implementation of Generics, basic introduction of Value Type and Reference Type, how null values are assigned to value types and how exception classes are used for handling exceptions.

Leave a Reply