Data Types

Choosing the correct data type for a variable is crucial as it specifies the size and type of values that it can hold. Using the wrong data type can result in errors, and waste time and memory.
In the C# programming language,there are three types of data:
  1. Value Data Type (int, float, char, bool, etc)
  2. Reference Data Type (class, object, string, and interface)
  3. Pointer Data Type (pointer)
It is crucial to choose the appropriate data type for each variable to enhance the readability and maintainability of your code. The most commonly used data types are:

Integer Types

int:

  • It can store integers such as positive numbers like 143 or negative numbers like -153.
  • Range: -2,147,483,648 to 2,147,483,647.
Example
int myNumber = 20000;
Console.WriteLine(myNumber);

long:

  • Handles larger whole numbers.
  • Range: -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.
Example
long myBigNumber = 30000000000L;
Console.WriteLine(myBigNumber);

Floating Types

float:

Use these for decimal values:
  • Stores fractional numbers (e.g., 8.29).
  • Precision: 6-7 decimal digits.
  • you should end the value with an “F”
Example
float myFloat = 8.34F;
Console.WriteLine(myFloat);

double:

  • More precise (e.g., 23.38).
  • Precision: About 15 decimal digits.
  • you should end the value with an “D”
Example
double myDouble = 34.67D;
Console.WriteLine(myDouble);

Character Type

char:

  • A character data type can store a single character, like ‘E’ or ‘#’.
  • It is always enclosed within single quotes.
Example
char myChar = 'E';
Console.WriteLine(myChar);

Boolean Type

bool:

  • This refers to values that can be either true or false.
Example
bool isSelected = true;
Console.WriteLine(isSelected);

String Type

string:

  • Stores sequences of characters (e.g., “Estudy247”).
  • Enclosed in double quotes.
Example
string myString = "Estudy 247";
Console.WriteLine(myString);
Post a comment

Leave a Comment

Scroll to Top