Skip to main content

 

C# - Data Types

Data types specify the type of data that a valid C# variable can hold. C# is a strongly-typed language. It means we must declare the type of a variable that indicates the kind of values it is going to store, such as integer, float, decimal, text, etc.

There are 3 types of data types in C# language.

Types

Data Types

Value Data Type

short, int, char, float, double, struct, enum, null

Reference Data Type

String, Class, Object,  Interface and array

Pointer Data Type

Pointers

 

 1. Value Data Types

Value data types hold the actual data in memory. These types include numbers, characters, booleans, and so on. C# supports both signed and unsigned versions of numeric types.

There are two kinds of value data types:

🔸 a) Predefined Data Types:

These are built-in types like int, char, float, double, etc. Here is a table showing their memory size and value range

 

Data Type

Size in C#

Value Range

Notes

byte

1 byte

0 to 255

Unsigned integer

sbyte

1 byte

-128 to 127

Signed integer

short

2 bytes

-32,768 to 32,767

Signed 16-bit integer

ushort

2 bytes

0 to 65,535

Unsigned 16-bit integer

int

4 bytes

-2,147,483,648 to 2,147,483,647

Signed 32-bit integer

uint

4 bytes

0 to 4,294,967,295

Unsigned 32-bit integer

long

8 bytes

-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

Signed 64-bit integer

ulong

8 bytes

0 to 18,446,744,073,709,551,615

Unsigned 64-bit integer

float

4 bytes

±1.5×10⁻⁴⁵ to ±3.4×10³⁸

Single-precision, ~7 digits accuracy

double

8 bytes

±5.0×10⁻³²⁴ to ±1.7×10³⁰⁸

Double-precision, ~15–16 digits accuracy

decimal

16 bytes

±1.0×10⁻²⁸ to ±7.9×10²⁸

High precision (28–29 digits), good for financial calculations

char

2 bytes

Unicode U+0000 to U+FFFF

Stores a single Unicode character

bool

1 byte (typically)

true or false

Represents Boolean values; size depends on implementation

int myNum = 5;               // Integer (whole number)

double myDoubleNum = 5.99D;  // Floating point number

char myLetter = 'D';         // Character

bool myBool = true;          // Boolean

string myText = "Hello";     // String

2) User defined Data Types

Structure: Structure (struct) is a value type that groups related variables. It can also include methods and constructors.

struct Coordinate

 {

public int x;

public int y;

}

enum: enum (or enumeration type) is used to assign constant names to a group of numeric integer values. It makes constant values more readable, for example, WeekDays.Monday is more readable then number 0 when referring to the day in a week.

An enum is defined using the enum keyword, directly inside a namespace, class, or structure. All the constant names can be declared inside the curly brackets and separated by a comma.

Example:

enum WeekDays

{

Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday

 }



2. Reference Data Types

Reference types don’t store the actual data but store a reference (or address) pointing to the data's memory location. If you change the value using one variable, the change reflects in all references to that data.

There are two categories:

  • Predefined types: like string, object

  • User-defined types: like class, interface

Example:

string myText = "Hello"; // Reference type

 3. Pointer Data Types

A pointer is a special type that stores the memory address of another variable. Pointers are mostly used in unsafe code blocks in C#.

Symbols used in pointer

Symbol

Name

Description

& (ampersand sign)

Address operator

Determine the address of a variable.

* (asterisk sign)

Indirection operator

Access the value of an address.


Declaring a pointer

The pointer in C# language can be declared using * (asterisk symbol).

1.      int * a;  //pointer to int      

2.      char * c; //pointer to char 


Comments

Popular posts from this blog

Function of OS 1. Process Management The operating system helps in running many programs at the same time. It keeps track of each running program (called a process), decides which one should run next, and stops or starts them as needed. It makes sure that all the programs get a fair chance to use the CPU.   2. Memory Management The OS manages the computer's memory (RAM). It decides which program will use how much memory and keeps track of it. When a program is closed, it frees up the memory so that other programs can use it. This helps the computer run smoothly without crashing.   3. File System Management The operating system helps us to create, save, open, and delete files. It organizes files in folders and keeps them safe. It also controls who can open or edit a file to protect our data.   4. Device Management The OS controls all the input and output devices like the keyboard, mouse, printer, and monitor. It tells the devices what to do and makes su...
   Structure of C# using System;              // Gives access to Console and other basic features   namespace MyFirstProgram       // Defines a namespace {     class Program              // Main class of the program     {         static void Main ( string [] args)   // Main method, starting point         {             Console.WriteLine( "Hello, World!" ); // Prints output to screen         }     } }   using System; This line allows your program to use predefined classes and functions in the System namespace. For example, Console.WriteLine() is part of System . It's like importing built-in tools so you c...