www.ManzoorTheTrainer.
com
Learn C#.Net Core From Scratch
With Real World Examples
Mohd Manzoor Ahmed
(MCT | Founder Of www.ManzoorTheTrainer.com)
www.ManzoorTheTrainer.com
Prerequisite
● You should know how to use computer.
● Install Visual Studio 2022 Community Edition
https://www.visualstudio.com/thank-you-downloading-visual-studio/?sku=Commu
nity&rel=15
www.ManzoorTheTrainer.com
.Net Flavours
● .Net Framework (1.0,2.0,3.0,3.5,4.0,4.5,4.6,4.7 <->)
● .Net Core (Our Course Focus)(1.0, 2.0, 2.1, 2.2, 3.0, 3.1…. 5,6,7,8) (.Net 8)
www.ManzoorTheTrainer.com
What is .Net 8 ?
● What can we do with .Net?
○ We can develop applications like Console App, Windows App, Web App,
Mobile App, Web services, Windows services, etc.,
● Why .Net?
○ Because it is a language and OS neutral platform.
● What is .Net?
○ It is a language and OS neutral platform on which we can develop any kind
of applications.
www.ManzoorTheTrainer.com
Language Neutral
● Same class library with different syntax
○ Console::WriteLine(L"C++ Hello, World!");
○ Console.WriteLine("C# Hello, World!");
○ Console.WriteLine ("VB Hello, World!")
.NET Framework 4.x
● It is our old framework enhanced that we have been using for more than a decade.
● It is not open source
● It can target only windows OS
.NET Core X.X (1, 2, 3, 5,6,7,8.…)
● It is a new framework similar to .net framework maintained by Microsoft and the
.NET community on GitHub.
● It is open source.
● It can target Windows, Linux & Mac OS.
C# & .Net Journey
“.Net was language neutral,
.Net Core is OS neutral.”
- www.ManzoorTheTrainer.com
www.ManzoorTheTrainer.com
Get Started With Our First C# Program
● Write a program in any text editor say notepad.
● Compile the program from developer command prompt.
● Run the program. ((csc /target:library Test.cs for dll))
Assembly
IL
First.cs Compile (CSC) Execute Output
MSIL
(.dll or .exe)
www.ManzoorTheTrainer.com
Demo
Let’s see a demo on C# First Program
www.ManzoorTheTrainer.com
Assemblies
● Assemblies are the building blocks of .NET applications; they form the
fundamental unit of deployment.
● An Assembly is a logical unit of code.
● Assembly physically exist as DLLs or EXEs.
● Every assembly file contains:
○ Information about itself called as Assembly Manifest.
○ Information about classes called as Metadata.
www.ManzoorTheTrainer.com
Demo
Let’s see a demo on Assemblies
www.ManzoorTheTrainer.com
Getting Started With Visual Studio 2022
● Getting familiar with projects targeting and visual studio 2022
● Write a hello world program.
● Understand Console.WriteLine() string formatting.
● Understand Console.ReadLine() string parsing.
● Writing a simple program for arithmetic operations.
● Understand Type Casting.
● Understanding how to debug a program.
www.ManzoorTheTrainer.com
Demo
Let’s see a demo on Getting Started
www.ManzoorTheTrainer.com
Data Types - Value Type
● Signed integral: sbyte, short, int, long
● Unsigned integral: byte, ushort, uint, ulong
● characters: char
● floating point: float, double
● Boolean: bool
● Enum types
● Struct types
● Nullable value types
Note : Ranges : https://msdn.microsoft.com/en-us/library/s3f49ktz.aspx
www.ManzoorTheTrainer.com
Data Types - Ref Type
● string
● Array
● Class : object
● Interface
www.ManzoorTheTrainer.com
var Vs dynamic
● var is static typed. It identifies the variable type at compile time itself.
○ var type is fixed with initialization
○ var x=”ManzoorTheTrainer”;
● dynamic is a runtime dispatch type. It identifies the variable type at runtime.
○ dynamic x=”ManzoorTheTrainer”;
○ dynamic type can changes where it is required
www.ManzoorTheTrainer.com
Demo
Let’s see a demo on var Vs dynamic
www.ManzoorTheTrainer.com
Control Structures
● Conditional Statements ● Iterative Statements
○ If ○ For
○ If else ○ While
○ Else if ladder ○ Do while
○ Switch case default ○ foreach
● UnConditional Statements
○ Goto
○ Continue
○ break
○ return
www.ManzoorTheTrainer.com
Demo
Let’s see a demo on Control Structures
www.ManzoorTheTrainer.com
Assignment
Write a C# program which takes current meter reading (CMR), previous meter
reading (PMR) and type of connection as input and displays the bill amount.
● Domestic consumers: ● Non-domestic/Commercial:
○ 0-200 units: Rs 5 per unit ○ 0-100 units: Rs 7.50 per unit
○ 201-300: Rs 7.20 per unit ○ 101-300: Rs 8.90 per unit
○ 301-400: Rs 8.50 per unit ○ 301-500: Rs 9.40 per unit
○ 401-800: Rs 9 per unit ○ Above 500: Rs 10 per unit
○ Above 800 units: Rs 9.50 per unit
www.ManzoorTheTrainer.com
Arrays
● It is a user defined collection of homogeneous data type elements.
● We use index to access an element of an array.
● When declaring an Array, specify the type, name, dimensions, and size.
● int[ ] myArray = { 5, 10, 15 };
● int[] myArray = new int[3];
○ myArray[0]=5;
○ myArray[1]=10;
○ myArray[2]=15;
● Int[,] my2dArray = new int[2,3];
www.ManzoorTheTrainer.com
Demo
Let’s see a demo on Arrays & foreach Loop
www.ManzoorTheTrainer.com
Structures
● It is a user defined collection of
heterogeneous data type elements.
● We use period operator to access
an element of the structure.
www.ManzoorTheTrainer.com
Structures
With Top Level
Statement
www.ManzoorTheTrainer.com
Demo
Let’s see a demo on Structures
www.ManzoorTheTrainer.com
Oriented Programming Through C#
Student
● Objects ● rollNo
○ Jack Properties ● name
● marks
○ Peter
Class
○ John
● getDetails()
Methods
● getResult()
www.ManzoorTheTrainer.com
Object & Class
● Object
○ Any real time entity is called as an object.
○ Every object consist of state(look and feel) and behaviour(what is does).
○ States are called as properties and behaviors are called as methods.
● Classes
○ Class is a blueprint of an object.
○ It consist of fields which are not allowed to access from outside the class.
○ It also consist of methods which are allowed to access from outside the class.
www.ManzoorTheTrainer.com
Fields
Defining A Class
● Defining A Class
● Defining Fields
● Defining Methods
Methods
● Public and Private specifiers
www.ManzoorTheTrainer.com
Demo
For Defining A Class
www.ManzoorTheTrainer.com
Object Creation
● Creating Object
● Accessing Method
www.ManzoorTheTrainer.com
Demo
For Object Creation
www.ManzoorTheTrainer.com
Constructor
● Why do i need constructor?
● Creating a constructor
www.ManzoorTheTrainer.com
Constructor
● It’s a special method with same name as class name
● Usually constructor is public.
● It won’t have any return type.
● It may or may not have parameters.
● Constructor with no parameters is called as default constructor.
● Constructor is invoked automatically when we create an object.
● You cannot invoke constructor explicitly.
● It is used to initialize an object.
www.ManzoorTheTrainer.com
Demo
For Constructor
www.ManzoorTheTrainer.com
Constructor Overloading
● If we have more than one constructor
with different parameters then we call
it as constructor overloading
www.ManzoorTheTrainer.com
Demo
For Constructor Overloading
Method Overloading
www.ManzoorTheTrainer.com
Compile Time or Static
Polymorphism
● If a class contains more than one
method with same name and different
parameters then we call this as method
overloading
www.ManzoorTheTrainer.com
Demo
For Method Overloading
www.ManzoorTheTrainer.com
this Keyword
● this keyword is a default object of current
class.
● In whatever class we use “this” keyword
it acts like its object
● ‘this’ keyword has two uses.
● ‘this’ keyword is used to refer the
members of same class.
● It is also used to invoke a constructor
from another constructor of the same
class.
www.ManzoorTheTrainer.com
Demo
For this Keyword
www.ManzoorTheTrainer.com
Latest Style Of Class
● Properties are special kind of methods to access
any field in a secured way. It contains two kinds of
methods i.e., getter and setter.
● Getter method is to read the value of the private
field. Setter method is to write the value of the
private field.
● It looks like we are accessing the fields directly.
But, internally it works as methods where we can
perform some kind of validations.
● With auto properties
● Latest style of object creation
www.ManzoorTheTrainer.com
Demo
For Latest Class Style
www.ManzoorTheTrainer.com
Static Variables
● Any member of a class declared with
the keyword static is called as static
member.
● Memory for static variable is allocated
only once and is shared by all the
objects of that class.
● We can access static variables directly
with the class name if they are public.
www.ManzoorTheTrainer.com
Demo
For Static Variables
www.ManzoorTheTrainer.com
Static Constructor
● Static constructor is a constructor
which is declared with the keyword as
static.
● Static constructor is use to initialize
static variables (private).
● Static constructor does not have any
access modifiers (i.e,Public,Private).
● Static constructor is parameter less.
● There can be one and only one static
constructor in a class.
● Static Constructor is invoked before
creation of any object.
www.ManzoorTheTrainer.com
Demo
For Static Constructor
www.ManzoorTheTrainer.com
Static Methods
● Static method is a method which is
declared with the keyword static.
● There can be n number of static
methods in a class.
● Static methods can access only static
variables.
● Static method can be directly accessed
by Class name.
www.ManzoorTheTrainer.com
Demo
For Static Methods
www.ManzoorTheTrainer.com
Static Class
● Static class is a class which is declared
with static keyword.
● Static class should contain only static
methods.
● Static class does not allow creation of
class reference or object.
● Like Console is a predefined static
class which contains all static methods
like WriteLine()
www.ManzoorTheTrainer.com
Demo
For Static Classes
www.ManzoorTheTrainer.com
Namespaces
● Namespaces are logical group of
classes and namespaces.
● It is used for avoiding name clashes
between two sets of code.
www.ManzoorTheTrainer.com
Demo
For Namespaces
www.ManzoorTheTrainer.com
Reusable Component Or .dll
UI / PLL BLL
Windows
App
Organization.dll
Web App Employee.cs
GetSalary()
Console
App
Note : Every UI/PLL will have a local copy of dll, any changes made dll will not be reflected
automatically
www.ManzoorTheTrainer.com
Organization.dll
● Create a new project Organization.
● Create a class Employee
● Create a method GetSalary()
● Create a Console UI
● Add dll reference
● Add namespace
● Create Object of Employee
● Call GetSalary
● public and internal class
www.ManzoorTheTrainer.com
Demo
For dll Consumption in ConsoleUI
www.ManzoorTheTrainer.com
Inheritance
● Deriving a new class from an existing
class is called Inheritance.
● Existing class is called as base class.
● New class is called as derived class.
● Everything from the base class gets
inherited except private members.
IS - A
www.ManzoorTheTrainer.com
Demo
For Inheritance
www.ManzoorTheTrainer.com
Types Of Inheritances
● Single Inheritance : If a derived class has a
single base class then it is called as single Single
inheritance i.e., B (Derived) → A(Base)
● Multilevel Inheritance : If a class is derived
from a class which is already a derived class
then it is called as multi level inheritance.
i.e., A(Base) ← B (Derived) ← C(Derived)
● Multiple Inheritance : If a class is derived
from more than one class then it is called as
multiple inheritance.i.e., A,B(Base) ←
C(Derived) Multilevel
● C# does not support multiple inheritance
using classes.
www.ManzoorTheTrainer.com
Demo
For Types Of Inheritance
www.ManzoorTheTrainer.com
Protected Members
● Protected members of a classes can be
inherited in the child class.
● Protected members of a class cannot
be accessed from outside of a class.
www.ManzoorTheTrainer.com
Demo
For Protected Members
Constructor Chaining (Or)
www.ManzoorTheTrainer.com
base keyword
● Any constructor of child class always
invokes default constructor of base
class.
● Calling the base class constructor
from the child class’ constructor using
base keyword is called as constructor
chaining.
● base keyword is a default object of its
base class.
● It is also used to refer the members of
base class from derived class.
www.ManzoorTheTrainer.com
www.ManzoorTheTrainer.com
Method Overriding
● If a method in the derived class has the
same signature as that of in the base
class with different implementation
then we say that method in the derived
class overrides the method of base class.
● This process is called as Method
Overriding.
● The method of the base class should be
declared as virtual and method in
derived class should be declared as
override.
www.ManzoorTheTrainer.com
Other Use Of base
● base keyword is also used to refer the
members of base class from derived
class.
www.ManzoorTheTrainer.com
Demo
For Method Overriding
www.ManzoorTheTrainer.com
Sealed Method & Class
● A sealed method is a method which is
declared with the keyword sealed and it
cannot be overridden by the derived class.
● But we can invoke this method.
● Sealed keyword is always used in
combination with override keyword.
● A sealed class is the class which is
declared with the keyword sealed and it
cannot be derived by other classes.
● But we can create the objects of sealed
class
www.ManzoorTheTrainer.com
Demo
For Sealed Methods And Classes
www.ManzoorTheTrainer.com
Abstract Method
● An abstract method is a method which
is declared with the keyword abstract.
● It does not have body.
● It should be implemented by the
derived class.
● If a method is abstract then the class
should be abstract.
www.ManzoorTheTrainer.com
Demo
For Abstract Method
www.ManzoorTheTrainer.com
Abstract Class
● It is the blueprint of the problem.
● It says what to do but not how to do.
● An abstract class is the class which is
declared with the keyword abstract.
● Basically it contains at least one
abstract method.
● It’s object cannot be created.
● But it’s reference can be created.
● It is used to implement runtime
polymorphism.
www.ManzoorTheTrainer.com
Demo
For Abstract Class
www.ManzoorTheTrainer.com
Runtime or Dynamic Polymorphism
● Deriving the abstract class by one or more classes and overriding all the abstract
methods.
● Creating the reference of abstract class and making it an object of derived class
based on user’s input.
● And calling the method of abstract class.
● The method gets referred from derived class at runtime based on user input.
● This complete process is called as runtime polymorphisms.
Runtime or Dynamic Polymorphism
www.ManzoorTheTrainer.com
Runtime or Dynamic Polymorphism
www.ManzoorTheTrainer.com
www.ManzoorTheTrainer.com
Demo
For Runtime Polymorphism
www.ManzoorTheTrainer.com
Interface
● An interface is a pure abstract class.
● It is a reference type and it contains only
abstract members.
● Any implementation must be placed in
class that implements them.
● All the member declarations inside
interface are implicitly public and
abstract.
● We can implement multiple inheritance
using interfaces.
www.ManzoorTheTrainer.com
Demo
For Interfaces
www.ManzoorTheTrainer.com
Partial Class
● Partial class is a class which is declared
with partial keyword.
● Its implementation can be spread over
multiple files.
● Multiple classes prefixed with partial
keyword with same class name and
different methods in different files, acts
as a single class with all the methods.
www.ManzoorTheTrainer.com
Demo
For Partial Classes
www.ManzoorTheTrainer.com
Struct vs Classes
● Structs Only:
○ Cannot support inheritance
○ Are value types
○ Are passed by value (like integers)
○ Cannot have a null reference (unless Nullable is used)
○ Do not have a memory overhead per new instance
● Classes Only:
○ Can support inheritance
○ Are reference (pointer) types
○ The reference can be null
○ Have memory overhead per new instance
www.ManzoorTheTrainer.com
Collection Classes
● Collection is an alternate way of storing
multiple variable number of elements or
objects.
● Like Stack, Queue, ArrayList , etc., are
collection classes present in
System.Collection namespace
● There is no type safety in collection
classes i.e., in a collection object of a
Queue we can store few integers and few
strings.
● We need to type cast the elements while
accessing it.
www.ManzoorTheTrainer.com
Demo
For Collection Classes
www.ManzoorTheTrainer.com
Generic Collection Classes
● Generic collection is an alternate way of
storing multiple variable number of
elements or objects.
● Like Stack, Queue, List , etc., are generic
collection classes present in
System.Collection.Generic namespace
● There is type safety in generic collection
classes i.e., in a generic collection object
of a Queue we cannot store few integers
and few strings.
● Hence it is strongly typed.
● We need not to type cast the elements
while accessing it.
www.ManzoorTheTrainer.com
Demo
For Generic Collection Classes
www.ManzoorTheTrainer.com
Exception Handling
● Runtime error is called as an exception.
● Exception handling is an in-built
mechanism in .NET core to detect and
handle runtime errors.
● C# provides three keywords try, catch
and finally to do exception handling.
● The try encloses the statements that
might throw an exception.
● The catch handles an exception if one
exists.
● The finally can be used for doing any
clean up process.
www.ManzoorTheTrainer.com
Demo
For Exception Handling
www.ManzoorTheTrainer.com
Thanks