Welcome to deBUG.to Community where you can ask questions and receive answers from Microsoft MVPs and other experts in our community.
0 like 0 dislike
309 views
in Blog Post by 29 53 84
edited by

In this post, we are gonna explore data types in C#.

C#4BeginnersData types in C#

  • C# is a strongly-typed language.
  • You must declare the type of variable that indicates the kind of values.
  • Date types makes your code more maintainable and readable.

There are two data types in C#

  • Value types: include  
    • Simple type (int, string, float, bool, long, char, etc)
    •  enum types, 
    • struct types, be
    • and Nullable value types.
  • Reference types: include class types, interface types, delegate types, and array types.

Value types

  • When data types hold a data value within its own memory space.
  • These data types are classified as value type
    • bool, 
    • byte, 
    • char, 
    • decimal, 
    • double, 
    • enum, 
    • float, 
    • int, 
    • long, 
    • sbyte, 
    • short, 
    • struct, 
    • uint, 
    • ulong, 
    • ushort.

Integer Types 

  • Data type alias: int
  • .Net type: System.Int32
  • Size: 4 bytes
  • Description: stores numbers (positive or negative) from -2,147,483,648 to 2,147,483,647

Ex:

int i = 12;

unsigned integers

  • Data type alias: uInt
  • .Net type: System.UInt32
  • Size: 4 bytes
  • Description: its range from 0 to 4,294,967,295

Ex:

uint ui = 100u;

Note that you should end the value with an "F"

long Types 

This is used when int data type is not large enough to store the value.

  • Data type alias: long
  • .Net type: System.Int64
  • Size: 64 bits
  • Description: stores numbers (positive or negative) from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.

Ex:

long l = 45755452222222l;

Ulong Types 

  • Data type alias: long
  • .Net type: System.UInt64
  • Size: 64 bits unsigned integer
  • Description:  its range from 0 to 18,446,744,073,709,551,615.

Ex:

ulong ul = 45755452222222ul;

Float Types 

The precision of float is only six or seven decimal digits.

  • Data type alias: float
  • .Net type: System.Single
  • Size: 4 bytes
  • Description: stores fractional numbers from 3.4e−038 to 3.4e+038

Ex:

float f = 5.75F;

Double Types 

double variables have a precision of about 15 digits.

  • Data type alias: double
  • .Net type: System.Double
  • Size: 8 bytes
  • Description: stores fractional numbers from 1.7e−308 to 1.7e+308.

Ex:

double d = 12.3;

Boolean Types 

bool type is a result of the comparison or equality operation. bool expression can be a controlling conditional expression in the conditional statements.

  • Data type alias: bool
  • .Net type: System.boolean
  • Description: Only take the values true or false.

Ex:

bool b = true;

Char Types 

char type must be surrounded by single quotes ' '.

The default value of the char type is \0, that is, U+0000.

  • Data type alias: char
  • .Net type: System.Char
  • Size: 16 bits
  • Description: The char data type is used to store a single character (U+0000 to U+FFFF).

Ex:

char fletter= 'A';

Reference Type

  • unlike value types, the reference type doesn't store the value directly.
  • It stores the address where the value is being stored.

These types are all of the reference type

  • strings
  • Arrays
  • Class
  • Delegetes 

string Types 

char type must be surrounded by single quotes '" ". for more details, please, check String in C#

  • Data type alias: string
  • Description: The string is used to store a text.

Ex:

string txt = "welcome to C#";

For Arrays, you can check Working with arrays in c#


Conclusion 

In this post, we have explored the value and reference data types in C#.

See Also


965 questions

628 answers

427 comments

192k users

تعلم بالعربي PowerApps

تعلم بالعربي Power Automate

If you don’t ask, the answer is always NO!
...