05 C# Data Types

And now we’re going to talk about C# Data Types and C# Type Conversion

In C#, variables are categorized into the following types:

  • Value types
  • Reference types
  • Pointer types

Value Types
Value type variables can be assigned a value directly. They are derived from the class System.ValueType. The value types directly contain data. Some examples are int, char, float, which stores numbers, alphabets, floating point numbers, respectively. When you declare an int type, the system allocates memory to store the value.

The kink below declares a table lists the available value types in C#

To get the exact size of a type or a variable on a particular platform, you can use the sizeof method. The expression sizeof(type) yields the storage size of the object or type in bytes. Following is an example to get the size of int type on any machine:

using system;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DataTypeApplication
    class Program
        static void Main(string[] args)
            Console.WriteLine("Size of int: {0}", sizeof(int));

When the above code is compiled and executed, it produces the following result:

Reference Types
The reference types do not contain the actual data stored in a variable, but they contain a reference to the variables.
In other words, they refer to a memory location. Using more than one variable, the reference types can refer to a memory location. If the data in the memory location is changed by one of the variables, the other variable automatically reflects this change in value. Example of built-in reference types are: object, dynamic and string.

The Object Type is the ultimate base class for all data types in C# Common Type System (CTS). Object is an alias for System.Object class. So object types can be assigned values of any other types, value types, reference types, predefined or user-defined types. However, before assigning values, it needs type conversion. When a value type is converted to object type, it is called boxing and on the other hand, when an object type is converted to a value type, it is called unboxing.

You can store any type of value in the dynamic data type variable. Type checking for these types of variables takes place at runtime. Dynamic types are similar to object types except that type checking for object type variables takes place at compile time, whereas that for the dynamic type variables takes place at run-time.

The String Type allows you to assign any string values to a variable. The string type is an alias for the System.String class. It is derived from object type.


Pointer Types
Pointer type variables store the memory address of another type. Pointers in C# have the same capabilities as in C or C++.
Syntax for declaring a pointer type is:

type*  identifier;

For Example:

char* cptr;
int* iptr;

And now we have already discussed the C# Data Types 🙂

So. it’s the time to know C# Type Conversions 

Type conversion is basically type casting or converting one type of data to another type. In C#, type casting has two forms:

  •  Implicit type conversion – these conversions are performed by C# in a type-safe manner. Examples are conversions from smaller to larger integral types and conversions from derived classes to base classes.
  •  Explicit type conversion – these conversions are done explicitly by users using the pre-defined functions. Explicit conversions require a cast operator.

The following example shows an explicit type conversion:

namespace TypeConversionApplication</pre>
class ExplicitConversion
static void Main(string[] args)
double d = 5673.74;
int i;
// cast double to int.
i = (int)d;


C# Type Conversion Methods
The link below declares the built-in type conversion methods:

The following example converts various value types to string type:

namespace TypeConversionApplication
     class StringConversion
         static void Main(string[] args)
             int i = 75;
             float f = 53.005f;
             double d = 2345.7652;
             bool b = true;


And now we have already finished our lesson 🙂
Next time we will talk about exciting topic so, Stay tuned 🙂
And if you need any help or support, Feel free to contact me 🙂


Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s