codescracker


c#

C# Data Types



« Previous Tutorial Next Tutorial »


In C# programming language, variables are simply categorized into these three types:

Value Type Variables in C#

The value type variables in C#, can be assigned a value directly. They are simply derived from the class named System.ValueType. For examples, int, char, float. Here, the following table lists the value types in C# programming:

Type Represents Range Default Value
char 16-bit Unicode character U +0000 to U +ffff '\0'
byte 8-bit unsigned integer 0 to 255 0
bool Boolean value True or False False
uint 32-bit unsigned integer type 0 to 4,294,967,295 0
int 32-bit signed integer type -2,147,483,648 to 2,147,483,647 0
decimal 128-bit precise decimal values with 28-29 significant digits (-7.9 x 1028 to 7.9 x 1028) / 100 to 28 0.0M
double 64-bit double-precision floating point type (+/-)5.0 x 10-324 to (+/-)1.7 x 10308 0.0D
float 32-bit single-precision floating point type -3.4 x 1038 to + 3.4 x 1038 0.0F
ushort 16-bit unsigned integer type 0 to 65,535 0
short 16-bit signed integer type -32,768 to 32,767 0
long 64-bit signed integer type -923,372,036,854,775,808 to 9,223,372,036,854,775,807 0L
sbyte 8-bit signed integer type -128 to 127 0
ulong 64-bit unsigned integer type 0 to 18,446,744,073,709,551,615 0

How to Get Exact Size of Type/Variable in C# ?

To get the exact size of type/variable in C# programming on the particular platform, use the sizeof() method. Here is an example, illustrates how to get the exact size of a type or a variable in C#:

/* C# Data Types - Example Program */
		
using System;
namespace GetSizeApplication
{
   class Sizeclass 
   {
      static void Main(string[] args)
      {
         Console.WriteLine("Size of int = {0}", sizeof(int));
         Console.ReadLine();
      }
   }
}

When the above code is compile and executed, it would produce the following output:

Size of int = 4

Reference Type Variables in C#

The reference types in C#, don't contain any actual data stored in the variable, but they contain reference to the variables. In other word, you can say, they refer simply to a memory location. Here are the examples of built-in reference types in C#:

Object Type in C#

Object type in C#, is the ultimate base class for all the data types in C# Common Type System (in short CTS). The object types in C#, can be assigned values of any other types, reference types, value types, or user-defined types.

However, before assigning the values, it needs a type conversion. When a value type is converted to object type, it is known as boxing, and when an object type is converted to value type, then it is known as unboxing. Here is an example

object ob;
ob = 10;	// boxing

Dynamic Type in C#

You are free to store any type of value in dynamic data type variable in C# programming. Here, type checking for these types of variables simply takes place at the program run-time. Here is the general form to declare a dynamic type in C# programming:

dynamic variable_name = value;

Here is a code fragment, showing dynamic type declaration in C#

dynamic d = 10;

String Type in C#

The string type allows you to assign the string values to a variable. You can assigned the value for a string type using the string literals in these two forms:

Here is an example

String strn = "Codes Cracker";

And a @quoted string literal looks like:

@"Codes Cracker";

Pointer Type Variables in C#

A pointer type variable store the memory address of another type. Here is the general form to declare pointer type in C#:

type *identifier;

Here is an example

char *cpt;
int *ipt;

« Previous Tutorial Next Tutorial »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test