C#

C# DataTypes

C# is a strongly typed language. This means that every variable must have a declared type.

string name = "Pirzada";
int age = 35;
bool isMarried = true;
decimal income = 24899.45m;
float hourlyRate = 20.8f;

Run Demo

The type of a variable specifies the type of data that it will store and, thus, determines the storage space allocated for the variable in the memory. The compiler uses type information to make sure that all operations in your code are type safe. That is, when you declare a variable of type int, the compiler allows you to use the variable in addition and subtraction operations. If you try to perform same operations on a variable of type string or bool, the compiler generates an error.

C# Divides Types into Two Sets

  • Predefined / Primitive types that the language offers. Also called built-in types.
  • User-defined types that the programmer defines.

Categories of Types

  • value types
  • reference types

Note: Any type that derives from a class named System.ValueType is a structure or enum, not a class and is automatically allocated on the stack.

The main difference between value and reference types is the manner in which their values are stored in memory.

Value types are stored in the program execution stack and directly contain their value, whereas a reference type stores a reference to the value in an area known as the managed heap (garbage-collected).

The table below represents the above-mentioned C# datatypes, their range, and their default values:

C# DataTypes

These are value types which consist of four signed integer types and four unsigned, three floating-point types as well as char and bool.

Description Size (bits) C# Type .NET Type Range/Precision Literal Suffix
Signed integers 8 sbyte System.SByte –128 to +127
16 short System.Int16 –32,768 to 32,767
32 int System.Int32 –2,147,483,648 to 2,147,483,647 (–2 billion to 2 billion)
64 long System.Int64 –9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 L or l
Unsigned integers 8 byte System.Byte 0 to 255
16 ushort System.UInt16 0 to 65,535
32 uint System.UInt32 0 to 4,294,967,295 (0 to 4 billion) U or u
32 uint System.UInt32 0 to 4,294,967,295 (0 to 4 billion) U or u
64 ulong System.UInt64 0 to 18,446,744,073,709,551,615 UL or ul
Floating point 32 float System.Single -3.4 × 1038 to 3.4 × 1038 7 digit precision F or f
64 Double System.Double ±5.0 × 10-324 to ±1.7 × 10308 15-16 digit precision D or d
128 decimal System.Decimal -7.9 × 1028 to 7.9 × 1028/ 100 to 28 28-29 digit precision M or m
Unicode character 16 char System.Char U +0000 to U +ffff
Boolean value 4 bool System.Boolean True or False

You need to decide which size integer to use (short, int, or long) based on the value you want to store. For most small fractional numbers, float is fine. Float, double, and decimal offer different degrees of size and precision.

One thing to note here is that the compiler assumes that any number with a decimal point is a double unless you tell it otherwise.

You can also use the MaxValue and MinValue properties of numerical types that the .NET supports to get the information regarding the range a given type can store.

Console.WriteLine($"Min value of int : {int.MinValue}");
Console.WriteLine($"Max value of int : {int.MaxValue}");
OUTPUT

Min value of int : -2147483648
Max value of int : 2147483647

bool

  • bool stands for Boolean values
  • Boolean values can be true or false
  • Default value is false
  • Boolean or conditional type is used to evaluate logical conditions
bool isMarried = true;
bool isValid = false;

char

char stands for character and represents a UTF-16 code unit to store simple, Unicode, or escape characters enclosed by single quote marks such as ‘A’, ‘%’, ‘@’, ‘\u0041’, and ‘\t’ etc.

char simple = 'A'; // Simple character
char uniCode = '\u0041'; // Unicode character
char escape = '\t'; // Escape character

string

String is a reference type, and it represents a set or sequence of characters (UTF-16 code units). String literals should be enclosed within double inverted commas.

string name = "Pirzada";

This creates a string object that contains “Pirzada”.

Strings are immutable, meaning the contents of a string object cannot be changed once the object is created. The compiler actually creates a new string object each time to hold the new sequence of characters when assigned.

int

  • int stands for an integer
  • Limited to whole numbers
  • Default value is 0
  • No decimal or fractional parts
  • Holds numbers from -2,147,483,648 to 2,147,483,647 ( -2 billion to 2 billion )
int num = 303;

Examples: 12, 303, -304, 3350

byte

  • byte also refers to integral numbers
  • Default value is 0
  • Holds numbers from 0 to 255

float

  • float refers to floating point numbers.
  • Suffix f or F
  • Default value is 0.0F
  • Numbers with decimal places
  • Holds numbers from -3.4 × 1038 to +3.4 × 1038
  • Precision of approximately 7 digits
float value = 1.23456789f;

Console.WriteLine(value); // Output
OUTPUT: Rounded to 7 digits

1.234568

Examples: 13.33, 5.2 and -9.12.

double

  • double is the default floating point data type.
  • Suffix d or D
  • Default value is 0.0D
  • Holds numbers from ±5.0 × 10-324 to ±1.7 × 10308
  • Precision of approximately 15-16 digits

decimal

  • decimal stores a decimal number
  • Suffix m or M
  • Smaller range than float and double
  • Default value is 0.0M
  • Holds numbers from -7.9 x 1028 to 7.9 x 1028
  • Greater precision of approximately 28-29 digits

C# Reference | Microsoft Docs

Sending
User Rating 0 (0 vote)
Tagged in :

Add Comment

Click here to post a comment

Send this to a friend