Syntax, Variables, Data Types, Operators, and Input Output
Learn the basic building blocks of C# including variables, types, expressions, interpolation, and console interaction.
Inside this chapter
- Variables and Data Types
- Type Categories
- Console Input and Output
- Operators and Expressions
- Type Inference with var
- Real-World Usage Snapshot
Series navigation
Study the chapters in order for the clearest path from C# syntax and OOP to modern .NET web development, data access, async programming, architecture, and advanced engineering practice. Use the navigation at the bottom to move smoothly through the full series.
Variables and Data Types
int age = 25;
double salary = 52000.75;
char grade = 'A';
bool isActive = true;
string name = "Alice";
C# is strongly typed and expressive. It gives developers a rich set of built-in types and clear syntax for declaring and initializing values.
Type Categories
| Category | Examples |
|---|---|
| Integral types | int, long, short, byte |
| Floating-point types | float, double, decimal |
| Character and text | char, string |
| Logical type | bool |
decimal is especially useful in money-related applications because it avoids some binary floating-point accuracy issues.
Console Input and Output
Console.Write("Enter your name: ");
string? userName = Console.ReadLine();
Console.WriteLine($"Hello, {userName}");
String interpolation with $"" is one of the most readable features in C#. Students should learn it early because it improves clarity.
Operators and Expressions
int a = 10;
int b = 3;
int sum = a + b;
bool result = a > b && b > 0;
int remainder = a % b;
C# supports arithmetic, relational, logical, assignment, null-coalescing, conditional, and many other operators. Students should understand precedence and keep expressions readable.
Type Inference with var
var city = "Berlin";
var count = 5;
var does not make C# dynamically typed. The compiler still determines the type statically. Good style means using var when it improves readability, not when it hides meaning.
Real-World Usage Snapshot
These basics appear everywhere from console apps to enterprise APIs. Correct type choices, readable expressions, and good output formatting help prevent confusion later in business logic and data processing code.