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
class
keyword. - Every C# program has a
Main
method, 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:
int age = 25;
string name = "John";
bool isActive = true;
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:
if (age >= 18)
{
Console.WriteLine("Adult");
}
else
{
Console.WriteLine("Minor");
}
Object-Oriented Programming (OOP) in C#
- C# supports object-oriented programming with classes, objects, inheritance, polymorphism, and encapsulation.
- Classes are defined with the
class
keyword and have properties, methods, and constructors.
Example:
class Person
{
public string Name;
public int Age;
public Person(string name, int age)
{
Name = name;
Age = age;
}
public void Introduce()
{
Console.WriteLine($"Hello, my name is {Name} and I am {Age} years old.");
}
}
- Objects are instances of classes created using the
new
keyword.
Example:
Person person = new Person("Alice", 30);
person.Introduce();
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:
class Employee : Person
{
public string Position;
public Employee(string name, int age, string position) : base(name, age)
{
Position = position;
}
public void Work()
{
Console.WriteLine($"{Name} is working as a {Position}.");
}
}
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
override
keyword) and interfaces.
Example:
class Manager : Employee
{
public Manager(string name, int age) : base(name, age, "Manager")
{
}
public override void Work()
{
Console.WriteLine($"{Name} is managing the team.");
}
}
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:
class Person
{
private string _name;
private int _age;
public string Name
{
get { return _name; }
set { _name = value; }
}
public int Age
{
get { return _age; }
set { _age = value; }
}
}
Interfaces
- Interfaces define contracts that classes can implement, ensuring a consistent structure and behaviour.
- In C#, interfaces are defined using the
interface
keyword and are implemented by classes using the:
symbol.
Example:
interface IDrive
{
void Drive();
}
class Car : IDrive
{
public void Drive()
{
Console.WriteLine("Driving a car.");
}
}
class Truck : IDrive
{
public void Drive()
{
Console.WriteLine("Driving a truck.");
}
}
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 thewhere
keyword.
Example:
class GenericList<T>
{
private List<T> _items = new List<T>();
public void Add(T item)
{
_items.Add(item);
}
}
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:
List<int> numbers = new List<int> { 1, 2, 3, 4, 5 };
var evenNumbers = from n in numbers
where n % 2 == 0
select n;
// Or using method syntax:
var evenNumbersMethod = numbers.Where(n => n % 2 == 0);
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.