What is the Variables in C# ?
Variables are are used to store data that can be manipulated and referenced throughout the program. In C#, variables must be declared with a specific type, which defines the kind of data they can hold. Here’s a detailed introduction to variables in C#, along with an example and line-by-line notes.
Declaration and Initialization
In C#, a variable must be declared before it can be used. The declaration specifies the variable’s type and name. Optionally, the variable can be initialized (assigned a value) at the time of declaration.
Syntax
type variableName = value;
Example
using System; namespace VariableExample { class Program { static void Main(string[] args) { int age = 30; // Integer variable double height = 5.9; // Double variable string name = "John"; // String variable bool isActive = true; // Boolean variable Console.WriteLine("Name: " + name); Console.WriteLine("Age: " + age); Console.WriteLine("Height: " + height); Console.WriteLine("Active: " + isActive); } } }
Line-by-Line Notes
using System;
- This directive includes the System namespace, which contains fundamental classes and base classes that define commonly-used data types, events, and event handlers.
namespace VariableExample
- Defines a namespace called “
VariableExample
” to organize the code and prevent naming conflicts.
class Program
- Defines a class named
Program
. In C#, classes are the basic building blocks for creating objects
static void Main(string[] args)
- The
Main
method is the entry point of the program. It isstatic
because it is called by the runtime without creating an instance of the class.void
indicates that the method does not return any value.string[] args
is an array of strings that can hold command-line arguments.
int age = 30;
- Declares an integer variable named
age
and initializes it with the value30
. int
: Data type that represents a 32-bit signed integer.age
: Name of the variable.30
: Initial value assigned toage
.
double height = 5.9;
- Declares a double-precision floating-point variable named
height
and initializes it with the value5.9
. double
: Data type for double-precision floating-point numbers.height
: Name of the variable.5.9
: Initial value assigned toheight
.
string name = "John";
- Declares a string variable named
name
and initializes it with the value"John"
. string
: Data type for a sequence of characters.name
: Name of the variable."John"
: Initial value assigned toname
.
bool isActive = true;
- Declares a boolean variable named
isActive
and initializes it with the valuetrue
. bool
: Data type for boolean values (true
orfalse
).isActive
: Name of the variable.true
: Initial value assigned toisActive
.
Console.WriteLine("Name: " + name);
- Outputs the value of the
name
variable to the console. Console.WriteLine
: Method that outputs text to the console."Name: " + name
: Concatenates the string"Name: "
with the value ofname
.
Console.WriteLine("Age: " + age);
- Outputs the value of the
age
variable to the console. "Age: " + age
: Concatenates the string"Age: "
with the value ofage
.
Console.WriteLine("Height: " + height);
- Outputs the value of the
height
variable to the console. "Height: " + height
: Concatenates the string"Height: "
with the value ofheight
.
Console.WriteLine("Active: " + isActive);
- Outputs the value of the
isActive
variable to the console. "Active: " + isActive
: Concatenates the string"Active: "
with the value ofisActive
.