Variables and Data Types
In C#, variables are used to store and manipulate data. Before using a variable, you need to declare its type and assign a value to it.
C# provides various data types to represent different kinds of information. Some of the commonly used data types are:
int
(integer): used for storing whole numbers.double
(floating-point): used for storing decimal numbers with floating-point precision.string
: used for storing text.bool
(boolean): used for storing boolean values (true
orfalse
).
Here's an example of declaring and initializing variables of different data types:
TEXT/X-CSHARP
1int age = 30;
2string name = "John Doe";
3double salary = 50000.50;
4bool isMarried = false;
xxxxxxxxxx
39
}
using System;
namespace DataTypesExample
{
class Program
{
static void Main(string[] args)
{
// Declaring and Initializing Variables
int age = 30;
string name = "John Doe";
double salary = 50000.50;
bool isMarried = false;
// Outputting Variables
Console.WriteLine("Name: " + name);
Console.WriteLine("Age: " + age);
Console.WriteLine("Salary: " + salary);
Console.WriteLine("Married: " + isMarried);
// Data Type Conversion
int num1 = 10;
double num2 = 5.5;
// Implicit Conversion
double result1 = num1;
Console.WriteLine("Implicit Conversion: " + result1);
// Explicit Conversion
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment