Topic : Data Types in C# Programming: Value Types | Pointer Types Reference Types |

Data type in C#:

While Writing Program in any Language, We Need to use Various Variables to Store Various Information. Variables are Nothing but Reserved Memory Locations to Store Values. This Means that We Create a Variable, We Reserve some Space in Memory.

We May Like to Store Information of Various Data Types Like Character, Wide Character, Integer, Floating point, Double Floating Point, Boolean etc. Based on the Data Type of a Variable, The Operating system Allocates memory and Decides what ca be stored in The Reserved Memory.

In Computer Programming, Information is Stored in A computer Memory With Different data types. We Must Know what it is to be Stored in a Computer Memory, Whether it is just a Digit or Character, A Small Number, or Decimal Number. Computer Memory is Organized in Bytes, And for these Variables with varying Information a Data types is Associated. Byte is the Minimum Amount of Memory in Declared with two entities, Its type and Name.



  • In C#, We Categorized All the Data types in Following Groups:

    » Value Types:
    » Reference Types:
    » Pointer Types:




C# Data type : infobrother

Value Type:

Value type Variables can be Assigned a Value Directly. Assigning one Value type variable to Another copies the Contained value. This differs form the assignment of Reference type variables, which Copies a Reference to the object but not the Object it self. They are Derived from the class System.ValueType.

The Value Types directly Contain Data. Some Examples are Int, Char, Float etc. Which stores numbers, Alphabets, and Floating point Numbers, Respectively. When we declare an Int type, the System allocates memory to store the Value.

The Following Tables shows the Default Values of Values type.



TypeRepresentsRangeDefault Value
BoolBoolean ValueTrue or FalseFalse
Byte8-bit Unsigned Integer0 to 2550
Char16-bit Unicode Characteru +0000 to u +fff'\0'
Decimal128-bit Precise Decimal values with 28-29 significant digits.(-7.9 x 1028 to 7.8 X 1028)0.0M
Double64-bit Double-precision Floating Point Type(+/-)5.0-324 to (+/-)1.7 x 103080.0D
Float32-bit Single-precision Floating point type -3.4 x 1038 to +3.4 x 10380.0F
Int32-bit Signed Integer type-2,147,483,648 to 2,147,483,6470
long64-bit signed Integer type-9,223,372,036,854,775,808 to 9,233,372,036,854,775,8070L
sbyte8-bit Signed integer type-128 to 1270
short16-bit signed integer type-32,768 to 32,7670
uint32-bit unsigned integer type0 to 4,294,967,2950
ulong64-bit Unsigned integer type0 to 18,446,744,073,709,551,6150
ushort64-bit unsigned integer type0 to 65,5350


We can Get the Exact size of type or Variable on a Particular platform, We can use an Operator sizeof() to get the actual size of any type. This operator yields the Storage size of the object or type in bytes. Following is an Example to get the size of data type on any machine:

using System;


namespace SizeOf
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Size of Int: {0}", sizeof(int));
            Console.WriteLine("Size of long: {0}", sizeof(long));
            Console.WriteLine("Size of Double: {0}", sizeof(double));
            Console.WriteLine("Size of sbyte: {0}", sizeof(sbyte));
            Console.WriteLine("Size of Short: {0}", sizeof(short));
            Console.WriteLine("Size of Uint: {0}", sizeof(uint));
            Console.WriteLine("Size of Ulong: {0}", sizeof(ulong));
            Console.WriteLine("Size of Ushort: {0}", sizeof(ushort));
            Console.WriteLine("Size of Float: {0}", sizeof(float));
            Console.WriteLine("Size of Byte: {0}", sizeof(byte));
            Console.WriteLine("Size of Decimal: {0}", sizeof(decimal));
     

            Console.ReadKey();
        }
    }
}


In Above Program, Data sizes might be difference from your. Its depends on the computer structure and the compiler. copy this code into your compiler and check whether its same or different from mine.



Pointer Type:

Pointers are the special type of Variables, which can store the Memory address of Another variables. They contain a Memory address, Not the Value of the Variable. Pointers in C# have the same capabilities as the pointers in C or C++. (Read more...)

Following are the Examples of Pointer type Declarations:


Syntax:Description:
int* PtrPtr is a pointer to an Integer.
int** PtrPtr is a pointer to a Pointer to an Integer.
int*[] PtrPtr is a Single-Dimensional array of Pointer to Integers.
char* PtrPtr is a pointer to Char.
void *PtrPtr is a Pointer to an Unknown Type.


Reference Type:

The Reference Type variables is such type of variable in C# that holds the reference of memory address instead of value. In other words, they refer to a Memory location. Using Multiple variables, 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 Type:

    The Object type is the Ultimate base class for all data types in C# Common type system (CTS). The Object is an Alias for System.Object class. The Object types can be Assigned values of any other types, value types, reference types, predefined or user-defined types. However, before assigning values, it Need type conversion.

    When a Value type is Converted to Object type, it is called Boxing, and when an object type is converted to a value type, it is called Unboxing.




    //Example:
    
    object obj;
    obj = 10;  // this is boxing:


    Dynamic Type:

    A Dynamic type escapes type checking at compile Time, Instead, it resolves type at Run time. It was a New feature in C# Introduce in C# 2010. This type is a static type, but an Object of type Dynamic bypasses static type checking. We can store any type of value in the Dynamic data type Variable. Type checking for these types of variables takes place at Run-time.



    //Syntax for Declaring a Dynamic Type is:
    
    dynamic <variable_Name> = value;
            
    //For Example:
            
    dynamic d = 20;

    Dynamic Types are Similar to Object types except that type checking for Object type variables take place at compile time, whereas that for the dynamic type variables takes place at Run time.



    String Type:

    The String Type allows us 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. The Value for a String Type can be assigned using string Literals in two forms.
    » Quoted

    string str = "C# Tutorials: InfoBrother";


    » @ Quoted

    @"C# Tutorials: InfoBrother";


















I Tried my Best to Provide you complete Information regarding this topic in very easy and conceptual way. but still if you have any Problem to understand this topic, or do you have any Questions, Feel Free to Ask Question. i'll do my best to Provide you what you need.

Sardar Omar.
InfoBrother





WRITE FOR INFOBROTHER

Advertising






Advertisement