C# Data Types

In this tutorial, we will learn about the various data types in C# programming language? Last updated : April 04, 2023

What are Data Types?

Data types define what and how data should be stored and used. C# is a type-safe language. Variables are declared as being of a particular type, and each variable is constrained to hold only values of its declared type In C#.

Types of C# Data Types

There are two types of data types are used,

  1. Value Types
    A variable of value types directly contains only an object with the value.
  2. Reference types
    A variable of reference type directly contains a reference to an object. Another variable may contain a reference to the same object.

We can also define our own value types by declaring enumerations or structures.

C# Predefined Data Types

The data types which are predefined in the C# compiler, these are also referred to as Basic Data Type of C#. The value range (minimum and maximum values) and size of these data types are predefined; anyone can not modify the range and size of these data types.

Most Common C# Data Types

The most common data types are:

Data Type Size (in bytes) Description (Range)
int4 bytes-2,147,483,648 to 2,147,483,647
long8 bytes-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
float4 bytesUsed to store fractional numbers up to 6-7 decimal digits
double8 bytesUsed to store fractional numbers up to 15 decimal digits
bool1 bittrue or false values
char2 bytesUsed to store a single character
string2 bytes per characterUsed to store a string (sequence of characters)

Here is the list of basic/predefined C# data types with type, size, range etc.

C# basic/predefined data types

Example of Value Types

Example of Value Types is: int A = 50; here variable A hold value 50 that can be used in program.

Example of Reference Types

We can define new reference types class, interface, and delegate declarations.

object ABC = new object();
ABC.myValue = 50;

Comments and Discussions!

Load comments ↻

Copyright © 2024 www.includehelp.com. All rights reserved.