C# Documntation
C# Documntation
1. Text-based interface: Interac on is done via keyboard inputs and textual output.
2. Minimal system resources: Console programs are lightweight and typically use less memory
and processing power.
3. Use of standard input/output: Input is received through the console, and output is printed
to the console.
Write the Code: Here is an example of a simple console program that takes user input, performs a
basic opera on, and outputs the result:
using System;
namespace ConsoleApp
class Program
Console.ReadKey();
}
}
Explana on of Code:
Classes in C#
In C#, a class is a blueprint for crea ng objects (instances). It defines the proper es (variables),
methods (func ons), and events that an object can have. Classes are a core concept of Object-
Oriented Programming (OOP).
1. Class Declara on: A class is declared using the class keyword, followed by the class name.
2. Proper es: Proper es hold the data of the object. In C#, proper es can have both get and
set accessors.
3. Methods: Methods define the behavior of the object. They can manipulate data or perform
ac ons.
Model = model;
Color = color;
Year = year;
o protected internal: Accessible within the same assembly and derived classes.
using System;
namespace CarExample
Model = model;
Color = color;
Year = year;
class Program
myCar.DisplayDetails();
1. Fields: Variables that hold data for a class. Fields are o en private and accessed through
public proper es.
{
private string model;
3. Events: Allow the class to no fy other parts of the program when an ac on occurs.
CarStarted?.Invoke(this, EventArgs.Empty);
1. Sta c Class:
2. Sealed Class:
o Cannot be inherited.
3. Abstract Class:
4. Normal Class:
{
public string Make { get; set; }
5. Interface:
void Drive();
1. Abstract Class
An abstract class cannot be instan ated and may contain abstract methods, which are
methods without implementa on. Derived classes are required to implement these abstract
methods.
Abstract classes help to provide a common interface for other classes and promote
reusability of code.
2. Polymorphism
Polymorphism allows a class or object to take on many forms. In C#, polymorphism can be
implemented through:
o Method Overloading: Defining mul ple methods with the same name but different
parameters.
Example of Method Overriding: A method in the base class can be overridden by a derived
class to provide more specific behavior.
3. Inheritance
Inheritance allows a class (derived class) to inherit proper es and behaviors (fields and
methods) from another class (base class).
The base class provides common func onality, while the derived class can extend or modify
that func onality.
Inheritance promotes code reuse and helps maintain a clear hierarchical structure.
4. Encapsula on
Encapsula on involves bundling data and methods that operate on that data into a single
unit (class).
It hides the internal data of the class and restricts direct access to it, ensuring that any
interac on with the data happens through public methods (ge ers and se ers).
This principle prevents unintended interference and misuse of the object's internal state.
Clear Structure: OOP promotes a clear and organized structure for programs, making them
easier to understand and manage.
Reusability: OOP enables the crea on of reusable components and applica ons with less
code, thus reducing development me and effort.
Maintainability: Since OOP encourages modularity, it’s easier to maintain, modify, and
debug programs. Addi onally, OOP helps keep code DRY (Don’t Repeat Yourself).
o Abstract classes and interfaces are used to implement abstrac on. They help in
hiding implementa on details and expose only the necessary features to the user.
o Example: An abstract Shape class defines a common method to calculate the area of
different shapes, while the derived classes (e.g., Circle, Rectangle) implement the
specific logic for each shape.
2. Polymorphism in Ac on:
o Method Overriding: A base class can define a method (like MakeSound()), and the
derived classes (Dog, Cat) can override this method to provide their specific
implementa ons.
o Method Overloading: Methods can have the same name but different parameter
types or numbers, providing flexibility in how they are called.
3. Inheritance and Reusability:
o Inheritance enables code reuse by allowing a derived class to inherit proper es and
behaviors from a base class.
o Example: A Vehicle class can be extended by Car and Truck classes, each adding more
specific features like the number of doors or payload capacity while s ll inheri ng
common func onality like DisplayInfo().
o Encapsula on ensures that an object's data is protected from direct access and
modifica on, allowing controlled interac on via methods (e.g., Deposit(),
Withdraw(), GetBalance()).
o Example: A BankAccount class encapsulates the balance field, and public methods
provide secure access to modify and retrieve the balance.
Conclusion:
Here’s a vivid documenta on style that brings the concepts to life with clear, concise explana ons,
and code examples:
1. Introduc on to C#
Modern, powerful features like garbage collec on, LINQ, and async/await.
Supports a wide range of development areas: apps, web, games, and more.
2. Basic Concepts in C#
Data Types:
Declara on & Ini aliza on: Variables hold data and are declared with a type.
Type Inference: Use var to allow the compiler to infer the type.
Enums: Enums make your code more readable by defining named constant values.
3. Control Flow
if (x > 10)
else
Switch Statement: A cleaner way to handle mul ple possible condi ons.
switch (day)
case "Monday":
break;
case "Friday":
break;
default:
Loops:
Console.WriteLine(i); // Output: 0, 1, 2, 3, 4
int count = 0;
Console.WriteLine(count); // Output: 0, 1, 2, 3, 4
count++;
int[] numbers = { 1, 2, 3, 4, 5 };
Console.WriteLine(number);
myCar.Model = "Tesla";
myCar.Year = 2023;
Methods & Func ons: Methods are blocks of code that perform a specific task.
return a + b;
Name = name;
}
Person p = new Person("John");
Inheritance: Allows a class to inherit proper es and methods from another class.
Console.WriteLine("Animal Sound");
Console.WriteLine("Bark");
}
}
Encapsula on: Restricts access to certain class members and provides controlled access.
return balance;
int[] numbers = { 1, 2, 3, 4, 5 };
Console.WriteLine(numbers[0]); // Output: 1
Lists & Dic onaries: Dynamic collec ons, including key-value pairs.
numbers.Add(4);
Console.WriteLine(numbers[3]); // Output: 4
{ "Alice", 85 },
{ "Bob", 90 }
};
Console.WriteLine(scores["Alice"]); // Output: 85
Console.WriteLine(integerBox.Item); // Output: 42
A console program is a type of applica on that interacts with the user through a text-based interface,
typically using the command line or terminal. Unlike graphical user interface (GUI) applica ons,
console programs rely on text input and output.
1. Text-based interface: Interac on occurs through keyboard inputs and textual output.
2. Minimal system resources: Console programs are lightweight and typically do not consume
much memory or processing power.
3. Standard input/output: Input is received via the console, and output is printed to the
console.
using System;
namespace ConsoleApp
class Program
// Display a message
Console.ReadKey();
Classes in C#
In C#, a class is a blueprint for crea ng objects (instances). It defines proper es (variables), methods
(func ons), and events that an object can have. Classes are fundamental to Object-Oriented
Programming (OOP).
2. Proper es: Variables that hold the data of an object, defining its state.
Model = model;
Color = color;
Year = year;
5. Access Modifiers: Control the visibility and accessibility of class members. Common
modifiers include:
o protected internal: Accessible within the same assembly and derived classes.
Example of a Class in C#
using System;
namespace CarExample
Model = model;
Color = color;
Year = year;
class Program
myCar.DisplayDetails();
}
}
Types of Classes in C#
1. Sta c Class:
return a + b;
2. Sealed Class:
3. Abstract Class:
o An abstract class cannot be instan ated and may contain abstract methods, which
must be implemented in derived classes.
Console.WriteLine("Ea ng food.");
}
Console.WriteLine("Bark!");
4. Normal Class:
o A normal class can be instan ated, contains instance and sta c members, and can be
inherited unless sealed.
5. Interface:
void Drive();
Console.WriteLine("Car is driving.");
Abstract Class: Cannot be instan ated directly. Used as a base class for other classes.
Normal Class: Can be instan ated, inherited, and contains both instance and sta c members.