Manage Data in a .NET Framework Application by Using System Types

As I promised yesterday, here is the first post about the 70-536 exam: Microsoft .NET Application Development Foundation.

There are two system types in .NET:

  • Value Types
  • Reference Types

Both of them inherit from the core type of the .NET Framework: System.Object. These types live in the System namespace. Now let’s see value types at work:

Value Types

Value Types fall into two main categories:

  • Structs
    • Numeric Types
      • Integers
      • Floating-point types
      • Decimals
  • Boolean
  • User defined structs
  • Enumerations

All Value Types contain values directly (not just the memory address of the given value). This means that when you assign a value type to another one, you create a copy of the given type, thus the original value won’t change.

All Value Types come with a default constructor, which assigns the default value for the type (in most cases, 0). It’s worth knowing the range of the given types. For example, the range of Int32, the default integer in .NET is from -231 to 231-1. On the exam, there may be questions about the values. Don’t worry, just use the supplied calculator, and remember that for example, Int32 is called this because it uses 32 bits to store an integer in memory.

By default, value types cannot store the null value. As a workaround, you can use Nullable types. To define a Nullable integer, just use:

Nullable <int> x = 0;

Or the shorthand:

int? x = 0;

When you define structs, you’ll face some limitations: structs cannot inherit from each other (but can implement interfaces), and cannot have a parameterless constructor (as they define one by default).

Reference Types

You can understand reference types in contrast of value types. Reference types are objects of the C# language. They contain the address of the pointed date, rather than the actual data itself. When you assign a reference type to another one, the pointer will be reassigned to the new memory location, so there won’t be any changes in the referenced data. Reference types can also inherit from each other, implement interfaces, etc.

Generic Types

An interesting feature of the C# language is the use of generics. Generics are classes which takes type parameters, and in this manner, they enforce type-safety to a great extent. Mostly you’ll use your generic types to store collections of a type, or define key-value types with them.

Exception Classes

Exception classes represents errors that occur during the execution of your applications. They help you provide a way to manage your application and get detailed information of an error if it occurs. You can raise exceptions by using the throw exception name syntax. You can catch them using a try/catch block. Remember that when using multiple catch directives, start from the most specified and end with the least, because the first catch that can handle your exception will execute. All exception classes inherit from the base Sysem.Exception class.


Attributes are a way to provide declarative information to your code. You can query for the existence of attributes at runtime by using reflection. Attributes add metadata to your code that your (and other) applications can view at runtime. Also, they are preferred to use when dealing with COM. You can apply attributes to almost everything (restrictions may apply with specific attributes). You do so by using the following syntax:

public string myString{get;set;}

All of your custom attributes must inherit form the System.Attribute class.

Boxing and UnBoxing

Boxing enables you to store a value type as if it were an object (reference type). This is an example of boxing:

int i = 2;
object o = (object)i

Unboxing is the opposite: it extracts the boxed value back into a value type. Syntax:

o = 123;
i = int(o);

Boxing and unboxing mean performance overhead, so try to avoid them. Make use of generics instead.

Further Readings

Reference Types
Value Types

Tags: , , ,

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: