0% found this document useful (0 votes)
14 views12 pages

Understanding C# Data Types Explained

Uploaded by

Donate
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views12 pages

Understanding C# Data Types Explained

Uploaded by

Donate
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Data Types in C#

Last Updated : 08 Sep, 2025

In C#, data types define the kind of values a variable can hold. Since C# is strongly typed, every
variable or constant must be declared with a specific data type, such as int, char or float.

Data Types in C# is Mainly Divided into 3 Categories:

 Value Data Types

 Reference Data Types

 Pointer Data Type

1. Value Data Types

In C#, the Value Data Types will directly store the variable value in memory and it will also accept
both signed and unsigned literals. The derived class for these data types are [Link].

Following are different Value Data Types in C# programming language

1.1 Signed & Unsigned Integral Types

There are 8 integral types which provide support for 8-bit, 16-bit, 32-bit and 64-bit values in signed
or unsigned form.

Alias .NET Type Type Size(bits) Range Default Value

sbyte [Link] signed integer 8 -128 to 127 0

-32768 to
short System.Int16 signed integer 16 0
32767

int System.Int32 signed integer 32 -231 to 231-1 0

long System.Int64 signed integer 64 -263 to 263-1 0L

unsigned
byte [Link] 8 0 to 255 0
integer
Alias .NET Type Type Size(bits) Range Default Value

System.UInt1 unsigned
ushort 16 0 to 65535 0
6 integer

System.UInt3 unsigned
uint 32 0 to 232 0
2 integer

System.UInt6 unsigned
ulong 64 0 to 263 0UL
4 integer

1.2 Floating Point Types

There are 2 floating point data types which contain the decimal point.

Alias .NET Type Size(bits) Range (aprox) Default Value

float [Link] 32 ±1.5 × 10-45 to ±3.4 × 1038 0.0F

doubl [Link]
64 ±5.0 × 10-324 to ±1.7 × 10308 0.0D
e e

 Float: It is 32-bit single-precision floating point type. It has 7 digit Precision. To initialize a
float variable, use the suffix f or F. Like, float x = 3.5F. If the suffix F or f will not use then it
is treated as double.

 Double: It is 64-bit double-precision floating point type. It has 14 - 15 digit Precision. To


initialize a double variable, use the suffix d or D. But it is not mandatory to use suffix
because by default floating data types are the double type.

1.3 Decimal Types

The decimal type is a 128-bit data type suitable for financial and monetary calculations. It has 28-
29 digit Precision. To initialize a decimal variable, use the suffix m or M. Like as, decimal x =
300.5m. If the suffix m or M will not use then it is treated as double.

Alias Type name Size(bits) Range (aprox) Default value

[Link]
decimal 128 ±1.0 × 10-28 to ±7.9228 × 1028 0.0M
l

1.4 Character Types


The character types represents a UTF-16 code unit or represents the 16-bit Unicode character.

Alias Type name Size In(Bits) Range Default value

cha
[Link] 16 U +0000 to U +ffff '\0'
r

Example 1: Program to demonstrate the above data types

using System;

namespace ValueTypeTest {

class GeeksforGeeks {

static void Main()

// declaring character

char a = 'G';

// Integer data type is generally used for numeric values

int i = 89;

short s = 56;

// long uses Integer values which may signed or unsingned

long l = 4564;

// UInt data type is generally used for unsingned integer values

uint ui = 95;

ushort us = 76;

// ulong data type is generally for unsingned integer values

ulong ul = 3624573;
// by default fraction value is double in C#

double d = 8.358674532;

// for float use 'f' as suffix

float f = 3.7330645f;

// for float use 'm' as suffix

decimal dec = 389.5m;

[Link]("char: " + a);

[Link]("integer: " + i);

[Link]("short: " + s);

[Link]("long: " + l);

[Link]("float: " + f);

[Link]("double: " + d);

[Link]("decimal: " + dec);

[Link]("Unsingned integer: " + ui);

[Link]("Unsingned short: " + us);

[Link]("Unsingned long: " + ul);

Output

char: G

integer: 89

short: 56

long: 4564

float: 3.733064

double: 8.358674532
decimal: 389.5

Unsinged integer: 95

Unsinged short: 76

Unsinged long: 3624573

Example 2: Sbyte signed integral data type

using System;

namespace ValueTypeTest {

class GeeksforGeeks {

static void Main()

sbyte a = 126;

// sbyte is 8 bit singned value

[Link](a);

a++;

[Link](a);

// It overflows here because byte can hold values from -128 to 127

a++;

[Link](a);

// Looping back within the range

a++;

[Link](a);

}
Output

126

127

-128

-127

Example 3: Program to demonstrate the byte data type

using System;

namespace ValueTypeTest {

class GeeksforGeeks {

static void Main()

byte a = 0;

// byte is 8 bit unsigned value

[Link](a);

a++;

[Link](a);

a = 254;

// It overflows here because byte can hold values from 0 to 255

a++;

[Link](a);

// Looping back within the range

a++;

[Link](a);
}

Output

255

1.5 Boolean Types

It has to be assigned either true or false value. Values of type bool are not converted implicitly or
explicitly (with casts) to any other type. But the programmer can easily write conversion code.

Alia
s Type Name Possible Values

bool [Link] true / false

Example: Using Boolean data type

using System;

namespace ValueTypeTest {

class GeeksforGeeks {

static void Main()

// boolean data type

bool b = true;

if (b == true)

[Link]("Hi Geek");

}
}

Output

Hi Geek

2. Reference Data Types

Reference data types in C# store the memory address (reference) of the actual data, not the data
itself. They are used for complex types like string, arrays, classes, interfaces and delegates.

2.1 String

It represents a sequence of Unicode characters and its type name is [Link]. So, string and
String are equivalent.

Example:

string s1 = "hello"; // creating through string keyword


String s2 = "welcome"; // creating through String class

2.2 Object

In C#, object is the base type from which all other types (value types and reference types) are
derived. Converting a value type to an object is called boxing and converting it back is called
unboxing.

Example:

using System;

namespace ValueTypeTest {

class Geeks {

static void Main()

// declaring string

string a = "Geeks";

// append in a

a += "for";

a = a + "Geeks";

[Link](a);
// declare object obj

object obj;

obj = 20;

[Link](obj);

// to show type of object using GetType()

[Link]([Link]());

Output

GeeksforGeeks

20

System.Int32

3. Pointer Data Type

The Pointer Data Types will contain a memory address of the variable value. To get the pointer
details we have a two symbols ampersand (&) and asterisk (*).

 ampersand (&): It is known as Address Operator. It is used to determine the address of a


variable.

 asterisk (*): It also known as Indirection Operator. It is used to access the value of an
address.

Syntax:

type* identifier;

// Valid syntax
int* p1, p;

// Invalid
int *p1, *p;

Example:

using System;

namespace Pointerprogram {
class GFG {

static void Main()

unsafe

// declare variable

int n = 10;

// store variable n address location in pointer variable p

int* p = &n;

[Link]("Value :{0}", n);

[Link]("Address :{0}", (int)p);

Output:

Value :10
Address :1988374520

Note:

This program will not work on online compiler.


Error: Unsafe code requires the `unsafe' command line option to be specified.
For its solution: Go to your project properties page and check under Build the checkbox Allow
unsafe code.

Suggested Quiz

4 Questions

Which suffix must be used when declaring a decimal literal in C#?

 A

d or D

 B
f or F

 C

m or M

 D

No suffix required

In C#, what determines the type of a variable declared with var?

 A

The compiler automatically sets it to object

 B

The type is always string

 C

The initial value assigned to the variable

 D

It has no type until runtime

Which of the following is true about the dynamic type in C#?

 A

Its type is checked at compile-time

 B

Its type is resolved only at runtime

 C

It does not allow method calls

 D

It is identical to var

Which of the following is the correct syntax for declaring a nullable integer?

 A

int? num = null;

 B

nullable int num = null;

 C

int num = null;

 D
var? num = null;

You might also like