CS Data Types

A data type defines what kind of data can be saved by a variable like integer, floating, character, and so on.

In C # language there are three kinds of data types.

 

C# Data Type - UK Academe

 

 

Types Data Types
Value Data Type short, int, char, float, double etc
Reference Data Type String, Class, Object and Interface
Pointer Data Type Pointers

 

The data value types are integral and float-based. C# supports signed as well as unsigned literals.

In C # language, there are two kinds of Value Data type.

  • Predefined Data Type
  • User Defined Data Type

 

Predefined Data Type

Predefined data are those that are supported natively by a programming language, such as Integer, Boolean, Float, etc.

User defined Data Type

User Defined Data types are those that are structured by users i.e. programmers for the sake of convenience such as Structure, Enumerations, etc.

Data types can vary in memory size by operating system in 32 or 64 bits.

Type Represents Range Default Value
bool Boolean value True or False False
byte 8-bit unsigned integer 0 to 255 0
char 16-bit Unicode character U +0000 to U +ffff '\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
int 32-bit signed integer type -2,147,483,648 to 2,147,483,647 0
long 64-bit signed integer type -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 0L
sbyte 8-bit signed integer type -128 to 127 0
short 16-bit signed integer type -32,768 to 32,767 0
uint 32-bit unsigned integer type 0 to 4,294,967,295 0
ulong 64-bit unsigned integer type 0 to 18,446,744,073,709,551,615 0
ushort 16-bit unsigned integer type 0 to 65,535 0

We can use the size method to obtain the exact size of a type or variable on a certain platform. The sizeof(type) expression returns the object or byte - in bytes.

Example:


using System;

namespace DataTypeApplication {
   class Program {
      static void Main(string[] args) {
         Console.WriteLine("Size of int: {0}", sizeof(int));
         Console.ReadLine();
      }
   }
}

 

The types of references do not include the actual information in a variable but contain a reference to the variables. In other words, it refers to a memory location. The reference types can refer to a memory location with multiple variables. If one variable changes the data in the memory position, this change in value is automatically reflected by the other variable.

The built-in reference types are: object, dynamic, and string.

Object

The Object Type is the ultimate base class in the Common Type System (CTS) for all data types. Object is a System.Object class alias. Object types can be assigned value of other types, value types, reference types, predefined or user-defined types. However, it needs conversion type before assigning values.

When a value type has been converted into the object type it is known as boxing and when the object type is converted to the value type, it is known as unboxing.

Example:


object obj;
obj = 200; // this is boxing

 

 

Dynamic Type

In the dynamic data type variable, we can store any kind of value. The type check is carried out in run - time for these kinds of variables.

Syntax of dynamic type declaration:

dynamic = value;

For Example:

dynamic d = 50;

Dynamic types are similar to object types except that the check for variables of object type takes place at compile time and for the dynamic type, at runtime. 

String Type

We can assign strings to any variable by String Type. The string type for the System. String class is an alias. It comes from the object type. The value for a string type can be assigned in two forms: quoted and @quoted using string literals.

For Example:


String str = "UK Academe";

A @quoted string looks like this


@"UK Academe";

 

Class, interface or delegate are user-defined reference types.

 

Pointer type variables save another type of memory address.The C# pointers are able to be the same as the C or C++ pointers.

Syntax to declare the pointer type


type* identifier;

For Example:


char* cptr;
int* iptr;