C# PART 2: UNDERSTANDING BASIC DATA TYPES AND VARIABLES IN C#
Please Subscribe Youtube| Like Facebook | Follow Twitter
DATA TYPES AND VARIABLES IN C#
In C#, variables are used to store data in the computer’s memory. In this article, we will discuss the basic data types and variables in C#.
Data Types in C#
A data type in C# specifies the type of data that a variable can hold. C# supports various data types such as integer, floating-point, character, boolean, etc. Let’s discuss them one by one.
Integer data type:
The integer data type is used to store whole numbers. In C#, there are several integer data types available such as byte, short, int, long, etc.
byte: It is an 8-bit unsigned integer that can store values from 0 to 255.
short: It is a 16-bit signed integer that can store values from -32,768 to 32,767.
int: It is a 32-bit signed integer that can store values from -2,147,483,648 to 2,147,483,647.
long: It is a 64-bit signed integer that can store values from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807.
Here’s an example code to declare and initialize integer variables in C#:
int a = 10;
long b = 10000000000;
Floating-point data type
The floating-point data type is used to store decimal numbers. In C#, there are two floating-point data types available such as float and double.
float: It is a 32-bit floating-point number that can store values with up to 7 digits of precision.
double: It is a 64-bit floating-point number that can store values with up to 15-16 digits of precision.
Here’s an example code to declare and initialize floating-point variables in C#:
float c = 3.14f;
double d = 3.14159265358979323846;
Character data type
The character data type is used to store a single character. In C#, the character data type is denoted by the char keyword.
Here’s an example code to declare and initialize character variables in C#:
char e = 'A';
Boolean data type
The boolean data type is used to store either true or false values. In C#, the boolean data type is denoted by the bool keyword.
Here’s an example code to declare and initialize boolean variables in C#:
bool f = true;
String data type
The string data type is used to store a sequence of characters or text. In C#, the string data type is denoted by the string keyword. Strings in C# are immutable, which means that the value of a string variable cannot be changed once it is initialized.
Here’s an example code to declare and initialize string variables in C#:
string g = "Hello, world!";
Variables in C#
A variable in C# is a named location in the computer’s memory that can hold a value. In C#, variables are declared using the syntax: data_type variable_name = initial_value;
Example Code
using System;
class Program
{
static void Main()
{
// integer data type
int num1 = 10;
int num2 = 20;
int sum = num1 + num2;
Console.WriteLine("The sum of {0} and {1} is {2}", num1, num2, sum);
// floating-point data type
float floatNum = 10.5f;
double doubleNum = 20.5;
Console.WriteLine("The value of floatNum is {0}", floatNum);
Console.WriteLine("The value of doubleNum is {0}", doubleNum);
// boolean data type
bool isTrue = true;
bool isFalse = false;
Console.WriteLine("The value of isTrue is {0}", isTrue);
Console.WriteLine("The value of isFalse is {0}", isFalse);
// character data type
char ch = 'A';
Console.WriteLine("The value of ch is {0}", ch);
// string data type
string str = "Hello, World!";
Console.WriteLine(str);
}
}
Output:
The sum of 10 and 20 is 30 The value of floatNum is 10.5 The value of doubleNum is 20.5 The value of isTrue is True The value of isFalse is False The value of ch is A Hello, World!
This code declares and initializes variables of different data types and then displays their values using the Console.WriteLine() method. The output shows the values of the variables on the console window.
C# Beginner Tutorial Series
- C# Part 1: Setup and Introduction
- C# PART 2: UNDERSTANDING BASIC DATA TYPES AND VARIABLES IN C#
- C# PART 3: OPERATORS AND EXPRESSIONS IN C#
- C# PART 4: CONTROL FLOW STATEMENTS IN C#
- C# Part 5: Methods In C#
- C# Part 6: Arrays In C#
- C# Part 7: String Manipulation In C#
- C# Part 8: Object-Oriented Programming In C# (Classes and Objects)
- C# Part 9: Object-oriented Programming In C# (OOP Pillars)
- C# Part 10: Exception Handling in C#