May 28, 2026
C# Fundamentals and Object-Oriented Programming
Introduction to C# and .NET
- C# is a versatile, object-oriented programming language developed by Microsoft.
- It is designed to work within the .NET framework, which supports cross-platform development and a wide range of libraries and tools.
Basic Syntax and Structure
- C# programs typically consist of one or more classes, which are defined using the
classkeyword. - Every C# program has a
Mainmethod, which is the application's entry point. - Statements are terminated with a semicolon (;).
- Comments can be single-line (
//) or multi-line (/* */).
Variables and Data Types
- Variables in C# must be declared with a data type, such as
int,float,double,char,string, orbool. - Variables can be assigned a value using the assignment operator (
=).
Example:
Operators
- C# supports various operators, including arithmetic (
+,-,*,/,%), comparison (==,!=,<,>,<=,>=), logical (&&,||,!), and assignment (=,+=,-=,*=,/=,%=).
Control Structures
- C# has several control structures, such as
if,else,switch,while,do-while, andfor.
Example:
Object-Oriented Programming (OOP) in C#
- C# supports object-oriented programming with classes, objects, inheritance, polymorphism, and encapsulation.
- Classes are defined with the
classkeyword and have properties, methods, and constructors.
Example:
- Objects are instances of classes created using the
newkeyword.
Example:
Inheritance
- Inheritance allows a class to inherit properties and methods from a base class, promoting code reuse and modularity.
- In C#, inheritance is defined using the
:symbol.
Example:
Polymorphism
- Polymorphism allows methods to have different implementations based on the object they are called on.
- In C#, polymorphism can be achieved through method overriding (using the
overridekeyword) and interfaces.
Example:
These are the key concepts covered in
the C# Fundamentals and Object-Oriented Programming topics for Week 1, Day 2. Below are some additional topics to be aware of:
Encapsulation
- Encapsulation is the practice of hiding an object's implementation details and exposing only what is necessary.
- In C#, encapsulation can be achieved through access modifiers (
public,private,protected, andinternal) and properties (getters and setters).
Example:
Interfaces
- Interfaces define contracts that classes can implement, ensuring a consistent structure and behaviour.
- In C#, interfaces are defined using the
interfacekeyword and are implemented by classes using the:symbol.
Example:
Generics
- Generics allow you to create type-agnostic classes, methods, and interfaces that work with various data types.
- In C#, generics are defined using angle brackets (
<>) and can be constrained using thewherekeyword.
Example:
LINQ (Language Integrated Query)
- Using a consistent syntax, LINQ allows you to query data from various sources (such as collections, databases, and XML files).
- In C#, LINQ queries can be written using query syntax or method syntax.
Example:
These concepts form the basis of C# programming and object-oriented programming. Be sure to practice and reinforce your understanding with hands-on exercises and projects.