Data Types in C#.

Data types in C# are fundamental building blocks that define the kind of data a variable can hold. As C# is a strongly typed language, variables and expressions must have consistent data types and type conversions are limited or controlled by the language rules. 

Before learning about different data types and their usage let us learn what is data type and why it is important.

Data Types in C#

What is Data Types?

In programming, data types specify the type of data that can be stored in a variable. They determine the size, format, and range of values that a variable can hold. C# provides a rich set of data types, which can be broadly categorized into two groups: 

  • Value Types 
  • Reference Types.


Value Types C#.

Value types represent data directly, and their values are stored directly in memory. When you work with a value type, you manipulate the actual data rather than a reference to it.

Here is the list of some common value types in C#:

  • Integer Types: int, unit, short, ushort, long, ulong.  
  • Floating-Point Types: float, double, decimal.
  • Boolean Type: bool.
  • Character Type: char.
  • Enumerations: A user-defined value type that consists of a set of named integral constants. 
  • Nullable Value Types: Allows value types to have a value of null in addition to their normal range of values.
  • User-Defined Types: You can also define your own data types using classes and structs, making C# highly extensible and suitable for complex data modeling.
Example:
// Integer Types 
int myInt = 42;
uint myUInt = 12345U;
short myShort = -12345;
ushort myUShort = 56789;
long myLong = 123456789012345L;
ulong myULong = 987654321012345UL;

// Floating-Point Types
float myFloat = 3.1415927f;
double myDouble = 2.718281828459045;
decimal myDecimal = 123.456m;

// Boolean Type
bool isTrue = true;
bool isFalse = false;

// Character Type
char myChar = 'A';



Reference Type in C#.

Reference types store references (memory addresses) to the actual data. When you work with a reference type, you manipulate a reference to the data rather than the data itself. 

Here are some common reference types in C#:
  • String Type: Represents a sequence of characters (a string of text).
  • Object Type: The base type for all other types in C#. It can hold values of any data type.
Example:
string myString = "Hello, World!";

object myObject = 42;

List of Data Types in C#.

Here's a summary of common data types in C#, along with their characteristics and valid value ranges:

Data Type Description Valid Range or Values
int 32-bit signed integer -2,147,483,648 to 2,147,483,647
uint 32-bit unsigned integer 0 to 4,294,967,295
short 16-bit signed integer -32,768 to 32,767
ushort 16-bit unsigned integer 0 to 65,535
long 64-bit signed integer -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
ulong 64-bit unsigned integer 0 to 18,446,744,073,709,551,615
float Single-precision float ±1.5 x 10^-45 to ±3.4 x 10^38
double Double-precision float ±5.0 x 10^-324 to ±1.7 x 10^308
decimal Decimal float ±1.0 x 10^-28 to ±7.9 x 10^28
bool Boolean value true or false
char Unicode character Any valid Unicode character
string Sequence of characters Any string of text
object Base type for all types Any C# data type
enum User-defined constants A set of named integral constants

⚡ Please share your valuable feedback and suggestion in the comment section below or you can send us an email on our offical email id ✉ algolesson@gmail.com. You can also support our work by buying a cup of coffee ☕ for us.

Similar Posts

No comments:

Post a Comment


CLOSE ADS
CLOSE ADS