Variables and Constants in C#
In C#, variables and constants are the foundation for storing and manipulating data within a program.
The main difference between the two is that variables can change their value during execution, while constants remain fixed.
Variable Declaration
A variable is a memory space that stores a value that can be modified.
It is declared by indicating the data type followed by the variable name:
int age = 25; // Integer variable
string name = "Ana"; // Text variable
bool isActive = true; // Boolean variable
Constant Declaration
A constant is defined with the keyword const.
Its value is assigned at the time of declaration and cannot change during execution.
const double PI = 3.1416;
const string Country = "Colombia";
If you try to modify a constant, the compiler will throw an error.
Naming Conventions
In C#, it is recommended to follow good practices:
-
Variables → camelCase notation:
myVariable,userCounter -
Constants → UPPERCASE with underscores:
MAX_VALUE,PI
Key Differences
| Feature | Variable | Constant |
|---|---|---|
| Value | Can change | Cannot change |
| Keyword | None special | const |
| Example | int age = 20; | const double PI = 3.1416; |
Printing Formats and Comments in C#
In C#, it is very common to show information to the user through the console, as well as documenting code with comments.
These two elements are key to writing clear and easy-to-understand programs.
Printing Formats in C#
Printing in the console is done with Console.WriteLine() or Console.Write().
Console.WriteLine(): prints the text and adds a line break at the end.Console.Write(): prints without a line break.
Basic Examples:
Console.WriteLine("Hello, world!");
Console.Write("This is printed on the same line.");
String Interpolation
A modern and clear way to display values inside text:
int age = 20;
string name = "Ana";
Console.WriteLine($"My name is {name} and I am {age} years old.");
$"" you can insert variables directly into the string with { }Placeholders (Format Indices)
Another way to print values is using indices:
string product = "Laptop";
double price = 1500.50;
Console.WriteLine("The product {0} costs ${1}", product, price);
Numeric Formats
C# allows applying formats to numbers:
double price = 1234.56;
Console.WriteLine("Standard price: {0}", price);
Console.WriteLine("Price with 2 decimals: {0:F2}", price);
Console.WriteLine("Currency price: {0:C}", price);
-
F2: 2 decimal places -
C: Currency format -
P: Percentage
Comments in C#
Comments serve to document code and are not executed.
Types of Comments:
- Single Line
// This is a single-line comment
- Multi-line
/* This is a comment
spanning multiple lines */
- XML Documentation Comments Used to document methods and classes.
/// <summary>
/// Calculates the area of a rectangle
/// </summary>
public int CalculateArea(int width, int height) {
return width * height;
}
Data Types in C#
In C#, data types define what kind of information a variable can store.
The most common ones to start with are:
- Text strings (
string) - Numbers (
int,double,float,decimal) - Logical values (
bool)
String (Text Strings)
A string is used to store text.
string name = "Ana";
string greeting = "Hello, " + name;
Console.WriteLine(greeting); // Prints: Hello, Ana
-
They are written inside double quotes
"". -
You can concatenate
(+)or interpolate($"") strings.
Example with interpolation:
Console.WriteLine($"Welcome {name}");
Numbers (Numeric Values)
There are different types of numbers in C#:
-
int→ whole numbers (positive or negative). -
double→ decimal numbers with high precision. -
float→ less precise decimals, requiring anfat the end. -
decimal→ used in financial calculations due to its high precision.
Example:
int age = 25;
double price = 199.99;
float height = 1.75f;
decimal salary = 1500.75m;
Console.WriteLine($"Age: {age}, Price: {price}, Height: {height}, Salary: {salary}");
Boolean (Logical Values)
A bool can only hold two values:
-
true -
false
It is heavily used in conditions.
bool isOfAge = true;
bool hasLicense = false;
Console.WriteLine($"Is of age: {isOfAge}");
Console.WriteLine($"Has license: {hasLicense}");
In conditionals:
if (isOfAge) {
Console.WriteLine("You can vote.");
} else {
Console.WriteLine("You cannot vote.");
}