C# Fundamentals
This section covers the fundamental building blocks of the C# programming language, essential for developing robust and efficient applications on the .NET platform.
Variables and Data Types
C# is a statically typed language, meaning that all variables must have a declared type. This helps catch errors at compile time and improves performance. Common data types include:
int
: For whole numbers.double
: For floating-point numbers with double precision.bool
: For boolean values (true
orfalse
).string
: For sequences of characters.char
: For single characters.
Example declaration:
int age = 30;
string name = "Alice";
bool isStudent = true;
double price = 19.99;
Operators
C# supports a wide range of operators for performing operations on variables and values:
- Arithmetic Operators:
+
,-
,*
,/
,%
(modulo) - Comparison Operators:
==
,!=
,<
,>
,<=
,>=
- Logical Operators:
&&
(AND),||
(OR),!
(NOT) - Assignment Operators:
=
,+=
,-=
,*=
,/=
Control Flow Statements
Control flow statements allow you to dictate the order in which code is executed. This is crucial for creating dynamic and responsive applications.
Conditional Statements
Use if
, else if
, and else
to execute code blocks based on certain conditions.
int score = 75;
if (score >= 90) {
Console.WriteLine("Excellent!");
} else if (score >= 70) {
Console.WriteLine("Good job!");
} else {
Console.WriteLine("Keep practicing.");
}
The switch
statement provides an alternative for multi-way branching.
Loops
Loops are used to repeat a block of code multiple times.
for
loop: For iterating a specific number of times.while
loop: For iterating as long as a condition is true.do-while
loop: Similar towhile
, but the code block executes at least once.foreach
loop: For iterating over elements in a collection.
// For loop example
for (int i = 0; i < 5; i++) {
Console.WriteLine($"Iteration: {i}");
}
// Foreach loop example
string[] fruits = {"apple", "banana", "cherry"};
foreach (string fruit in fruits) {
Console.WriteLine(fruit);
}
Methods (Functions)
Methods are blocks of reusable code that perform a specific task. They help in organizing code and promoting modularity.
public class Calculator {
public int Add(int a, int b) {
return a + b;
}
}
// Calling the method
Calculator calc = new Calculator();
int sum = calc.Add(5, 3); // sum will be 8
Methods can have parameters and return values. The access modifiers (public
, private
, etc.) determine their visibility.
Classes and Objects
C# is an object-oriented programming (OOP) language. The core concepts are classes and objects.
- Class: A blueprint or template for creating objects. It defines properties (data members) and methods (behavior).
- Object: An instance of a class.
Understanding classes and objects is fundamental to C# development. They enable encapsulation, inheritance, and polymorphism.
public class Person {
public string Name { get; set; }
public int Age { get; set; }
public void Greet() {
Console.WriteLine($"Hello, my name is {Name} and I am {Age} years old.");
}
}
// Creating an object
Person person1 = new Person();
person1.Name = "Bob";
person1.Age = 25;
person1.Greet(); // Output: Hello, my name is Bob and I am 25 years old.
Namespaces
Namespaces are used to organize code and prevent naming conflicts. The using
directive allows you to use types from other namespaces without fully qualifying them.
using System; // Makes Console class available
using System.Collections.Generic; // Makes List class available
public class Example {
public static void Main(string[] args) {
Console.WriteLine("Working with namespaces.");
List names = new List();
names.Add("Charlie");
}
}
This section provides a foundational understanding of C#. For more advanced topics, explore the subsequent documentation modules.