0% found this document useful (0 votes)
24 views

Unit 1 Intro to DOT NET Architecture

.NET Technologies course focuses on teaching students about the Microsoft .NET Framework and its various components, including C#, web applications, and database interactions. Key objectives include understanding .NET architecture, creating web applications using MVC, and developing APIs. The course also covers the differences between .NET and .NET Framework, programming languages supported, and practical applications like WinForms, ASP.NET, and ADO.NET.

Uploaded by

paxawek190
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

Unit 1 Intro to DOT NET Architecture

.NET Technologies course focuses on teaching students about the Microsoft .NET Framework and its various components, including C#, web applications, and database interactions. Key objectives include understanding .NET architecture, creating web applications using MVC, and developing APIs. The course also covers the differences between .NET and .NET Framework, programming languages supported, and practical applications like WinForms, ASP.NET, and ADO.NET.

Uploaded by

paxawek190
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 74

.

NET Technologies
(01CE1602)
6th Semester
4 Credits

Prof. Ravikumar R N
Dept. of Computer Engineering
 Net Technologies are blend of technologies supported
by Microsoft .Net Framework that allows user to create
Objectives various applications.
 Students will be able to work with various technologies
provided by Microsoft .NET platform.
 To Review the components of .Net Framework
 To practice Web based application
Course  To create web applications using MVC framework
Outcomes  To practice basic database application using ADO.net
 To designing, developing, and deploying APIs
Unit 1
Introduction to .NET Framework and C#
 Introduction to .NET Framework Architecture
 CLR
 Assemblies
 Basics of C#
 Class
 Object
 Method
Contents
 Access Modifiers
 Constructors
 Abstract Class
 Inheritance
 Interface
 Polymorphism
 Exception Handling
 The .Net framework was meant to create applications,
which would run on the Windows Platform. The first
version of the .Net framework was released in the year
2002. The version was called .Net framework 1.0.
History of  The Microsoft .Net framework has come a long way
since then, and the current version is .Net Framework
.NET 6.0.
 The Microsoft .Net framework can be used to create
both – Form-based and Web-based applications. Web
services can also be developed using the .Net
framework.
 .NET Framework is a software development framework
for building and running applications on Windows.
 .NET Framework is part of the .NET platform, a
collection of technologies for building apps for Linux,
macOS, Windows, iOS, Android, and more.
What is .NET  .NET apps can run on many operating systems, using
different implementations of .NET (.NET Framework,
Framework? .NET Core or .NET, and Xamarin/Mono).
 The framework includes a variety of programming
languages, such as C#, F#, and Visual Basic, and
supports a range of application types, including
desktop, web, mobile, and gaming applications.
 . NET Framework supports more than 60 programming
languages of which 11 programming languages are
designed and developed by Microsoft.
 C#.NET
 VB.NET
 C++.NET
Eleven  J#.NET
Programming  F#.NET
Languages  JSCRIPT.NET
developed by  WINDOWS POWERSHELL
 IRON RUBY
Microsoft
 IRON PYTHON
 C OMEGA
 ASML(Abstract State Machine Language)
 .NET is Cross-Platform and runs on Linux, macOS, and
Windows OS. .NET Framework only runs on Windows
What is the OS.
difference  .NET is Open-Source and accepts contributions from
between .NET the community. The .NET Framework source code is
available but does not take direct contributions.
and .NET
 .NET Framework is included in Windows and
Framework? automatically updated machine-wide by Windows
Update. .NET is shipped independently.
 WinForms: This is used for developing Forms-based
applications, which would run on an end-user machine.
Notepad is an example of a client-based application.
 ASP.NET (Active Server Pages): This is used for
developing web-based applications, which are made to
Applications run on any browser such as Edge, Chrome or Firefox.
ASP.NET is a web framework designed and developed
Developed by Microsoft.
using .NET  ADO.NET (ActiveX Data Object): This technology is
Framework used to develop applications to interact with databases
such as Oracle or Microsoft SQL Server.
 WCF (Windows Communication Foundation): It is a
framework for building service-oriented applications.
Using WCF, you can send data as asynchronous
messages from one service endpoint to another.
 LINQ (Language Integrated Query): It is a query
language, introduced in .NET 3.5 framework. It is used
to make the query for data sources with C# or Visual
Basics programming languages.
Applications  Entity Framework: It is an ORM (Object Relational
Mapping)-based open-source framework that is used
Developed to work with a database using .NET objects. It
using .NET eliminates a lot of developers’ effort to handle the
database. (We learned Hibernate Framework in AJT).
Framework  Parallel LINQ: Parallel LINQ or PLINQ is a parallel
implementation of LINQ to objects. It combines the
simplicity and readability of LINQ and provides the
power of parallel programming. Fast speed execution
of LINQ queries.
Architecture of
.NET
Framework
 .NET applications are written in the C#, F#, or Visual
Basic programming language.
 Code is compiled into a language-independent Common
Intermediate Language (CIL). Compiled code is stored in
assemblies—files with a .dll or .exe file extension. It is
Architecture of called as MSIL (assembly code).
.NET  When an app runs, the CLR takes the assembly and uses
a just-in-time compiler (JIT) to turn it into machine code
Framework that can execute on the specific architecture of the
computer it is running on.
 The Common Language Runtime (CLR) is the execution
engine that handles running applications. It provides
services like thread management, garbage collection,
type-safety, exception handling, and more.
Components
of .NET
Architecture
 Common Language Specification (CLS) helps in easy creation of
the apps and software. Common Language Specification also
helps in reduction of cost. It helps in reducing the development
time and cost.
 The Common Language Infrastructure (CLI) is an app
development framework that is language-independent.
Therefore, developers don’t have to worry about changing the
Components language or syntax of their source code when switching from
of .NET one platform to another.
Architecture  Common Type Specification (CTS) is a language supporting
multiple data types is known as a common type system.
 CLR
 Framework Class Library (FCL) The FCL is part of the Common
Language Runtime. It is loaded when the program starts and
unloaded when the program ends. This loading and unloading
process is also known as “Dependency Injection”.
 Interoperability – Migrating lower version application to
higher version is easy.
 Portability – works on any windows platforms.
 Security - inbuilt security mechanism helps in both
Advantages of validation and verification of applications.
.NET  Memory management – CLR will take care.
 Simplified deployment - The .Net framework also have
tools, which can be used to package applications built on
the .Net framework. These packages can then be
distributed to client machines.
Step 1: File > New Project > C# Class Library
using System;
namespace ClassLibrary1
{
public class Class1

A Demo on {
public string DisplayString(string GetString)
Language {
Interoperability string str = "Hi!! I am from C#.NET " + GetString;
return str;
}
}
}
Step 2: Right click on solution Class Library > Add > New Project > Visual Basic >
Windows Form App

Step 3: Add Reference


Right click on WinFormsApp1 > Add > Project Reference > Select the
ClassLibrary dll file
A Demo on
Language Step 4: Open Form1.vb and double click
Public Class Form1
Interoperability Private Sub Form1_Load(sender As Object, e As EventArgs) Handles
MyBase.Load
Dim obj As New ClassLibrary1.Class1
MessageBox.Show(obj.DisplayString("Hello I am from VB.NET"), "RK")
Me.Close()
End Sub
End Class
Step 5: Right click on VB WinFormApp project and build > You will get
error

A Demo on
Language
Interoperability Step 6: Right click on VB WinFormApp > select “set as startup project” >
build and run
// Create a simple LINQ query.
using System;
using System.Linq;
class SimpQuery {
static void Main() {
int[] nums = { 1, -2, 3, 0, -4, 5 };

// Create a query that obtains only positive numbers.


var posNums = from n in nums
LINQ DEMO where n > 0
select n;
Console.Write("The positive values in nums: ");
// Execute the query and display the results.
foreach(int i in posNums) Console.Write(i + " ");
Console.WriteLine();
} This program produces the following output:
} The positive values in nums: 1 3 5
 C# (C-Sharp) is a simple, modern, general-purpose,
object-oriented programming language developed by
Microsoft within its .NET initiative led by Anders
Hejlsberg.
 C# is used to develop web apps, desktop apps, mobile
Basics of C# apps, games and much more.
 C# enables developers to build many types of secure
and robust applications that run in .NET. C# has its
roots in the C family of languages and will be
immediately familiar to C, C++, Java, and JavaScript
programmers.
 Simple - C# is a user-friendly language that offers a structured
approach to problem-solving. it provides a wide range of
library functions and data types to work.
 Modern Programming Language - C# programming is a
popular and powerful language that is for creating scalable,
Features of C# interoperable, and robust applications.
 Object Oriented - C# is an object-oriented programming
language, which makes development and maintenance easier.
 Type Safe - The code is type safe can only access memory
locations that it has permission to execute. This feature
significantly enhances program security.
 Interoperability - The interoperability process allows
C# programs to perform all the tasks that a native C++
application.
 Scalable and Updateable - C# is a programming
language that is scalable and can be updated
automatically.
 Component Oriented - It is widely used as a software
Features of C# development methodology to create applications that
are more strong and can easily scale.
 Structured Programming Language - divide programs
into parts using functions, making it easy to
understand and modify.
 Fast Speed - The compilation and execution time of C#
language is fast.
using System;
namespace HelloWorld
{
class Program
{
static void Main(string[] args)
{
Hello World Console.WriteLine("Hello World!");
Program using }
C# }
}
• The using System line means that you are using the System
library in your project. Which gives you some useful classes like
Console or functions/methods like WriteLine .
• The namespace HelloWorld is something that identifies and
encapsulates your code within that namespace. It's like
packages in Java. Organize classes.
 Everything in C# is associated with classes and objects, along
with its attributes and methods. For example: in real life, a car is
an object. The car has attributes, such as weight and color, and
methods, such as drive and brake.
 A class is a data structure that may contain data members
(constants and fields), function members (methods, properties,
events, indexers, operators, instance constructors, finalizers, and
static constructors), and nested types.
C# Classes and  A Class is like an object constructor, or a "blueprint" for creating
objects.
Objects
 In C#, classes support polymorphism, inheritance and also
provide the concept of derived classes and base classes.
 A class can be public or internal etc. By default modifier of the
class is internal.
 To create a class, use the class keyword:
class Car {
string color = "red";
}
 Objects are also known as instances and are created at
runtime.
 Object is a basic unit of Object-Oriented Programming and
represents real-life entities. A typical C# program creates
many objects, which as you know, interact by invoking
C# Classes and methods. An object consists of :
Objects  State: It is represented by attributes of an object. It also
reflects the properties of an object.
 Behavior: It is represented by the methods of an object. It
also reflects the response of an object with other objects.
 Identity: It gives a unique name to an object and enables
one object to interact with other objects.
using System;
class Car {
string color = "red";
static void Main(string[] args) {
C# Classes and Car myObj = new Car();
Objects
Console.WriteLine(myObj.color);
}
}
 A method is a block of code which only runs when it is
called.
 You can pass data, known as parameters, into a method.
 Methods are used to perform certain actions, and they are
also known as functions.
 Why use methods? To reuse code: define the code once,
C# Methods and use it many times.

class Program {
static void MyMethod() {
// code to be executed
}
}
static void MyMethod(string fname) {
Console.WriteLine(fname + " Welcome to MU");
}
static void Main(string[] args) {
C# Methods MyMethod("Ram");
Parameters
MyMethod("Sofi");
and
MyMethod("Aarav");
Arguments
}
When a parameter is passed to the method, it is called an
argument. So, from the example above: fname is a
parameter, while Ram, Sofi and Aarav are arguments.
 Access modifiers are accessibility levels of types and type
members and set rules how a type and its members can be
used within the type, current assembly, and other assemblies.
Access
Scope
Modifier

public The code is accessible for all classes.

private The code is only accessible within the same class.


Access The code is accessible within the same class, or in a class that
Modifiers protected is inherited from that class.
The code is only accessible within its own assembly, but not
internal from another assembly.
The type or member can be accessed by any code in the
protected
assembly in which it is declared, or from within a derived
internal class in another assembly.
private The type of member can be accessed within the same class
protected or the classes derived within the same and another assembly.
 In C#, an assembly is a collection of resources and types
What is that work together to form a functional unit. It can refer to
Assembly? a dynamic link library (DLL) or an executable file (.exe).
using System;
namespace MyApplication
{
class Student
{
public string name = "MU";

public void print()


{
Console.WriteLine("Hello from Student class");
}
Public Access }
Modifier class Public
{
static void Main(string[] args)
{
Student student1 = new Student();

Console.WriteLine("Name: " + student1.name);

student1.print();
Console.ReadLine(); Name: MU
} Hello from Student class
} }
using System;
namespace MyApplication1
{
class Student
{
private string name = "MU";

private void print()


{
Console.WriteLine("Hello from Student class");
}
}
Private class Program
{
static void Main(string[] args)
{

Student student1 = new Student();


Console.WriteLine("Name: " + student1.name);
student1.print();
Console.ReadLine();
}
} Error CS0122 'Student.name' is inaccessible due to its protection level
} Error CS0122 'Student.print()' is inaccessible due to its protection level
using System;
namespace MyApplication11
{
class Student
{
protected string name = "RK";
}

// derived class
class Program : Student
{
Protected static void Main(string[] args)
{

// creating object of derived class


Program program = new Program();

// accessing name field and printing it


Console.WriteLine("Name: " + program.name);
Console.ReadLine();
}
} Name: RK
}
using System;
namespace Assembly
{

class Student
{
internal string name = "MU";
}

class Program
{
static void Main(string[] args)
Internal {

// creating object of Student class


Student theStudent = new Student();

// accessing name field and printing it


Console.WriteLine("Name: " + theStudent.name);
Console.ReadLine();
}
} Name: MU
}
Note: It will not allow access from another assembly.
// Code on Assembly2
// Code on Assembly1 using System;
using System;
// access Assembly1
namespace Assembly1 { using Assembly1;

public class StudentName { namespace Assembly2 {


internal string name = “MU"; class Program {
Internal from } static void Main(string[] args) {
another class Program {
StudentName student = new StudentName();

Assembly static void Main(string[] args) {


}
// accessing name field from Assembly1
Console.Write(student.name);
} Console.ReadLine();
} }
}
}

Error CS0122 'StudentName.name' is inaccessible due to its protection level


// Code on Assembly1
using System;
namespace Assembly1
{
public class Greet
{
protected internal string msg = "Hello";
}
class Program // Code on Assembly2
{ using System;
static void Main(string[] args)
{ // access Assembly1
Protected Greet greet = new Greet();
Console.WriteLine(greet.msg);
using Assembly1;

Internal Console.ReadLine();
}
namespace Assembly2
{
} // derived class of Greet
} class Program : Greet
{
static void Main(string[] args)
Output: {
Program greet = new Program();
Console.Write(greet.msg);
Hello Console.ReadLine();
}
} Output:
} Hello
using System;
namespace Assembly1
{
public class StudentName
{
private protected string name = "MU";
}
class Program1 : StudentName
{
static void Main(string[] args)
Private {
Program1 student = new Program1();
Protected // accessing name field from base class
Console.Write(student.name);
Console.ReadLine();
}
}
}

Output:
MU
Caller's location public protected protected internal private private
internal protected
Within the class ✔️ ✔️ ✔️ ✔️ ✔️ ✔️

Derived class
✔️ ✔️ ✔️ ✔️ ✔️ ❌
(same assembly)

Access Non-derived class


✔️ ✔️ ❌ ✔️ ❌ ❌
Modifiers (same assembly)

Summary Derived class


(different ✔️ ✔️ ✔️ ❌ ❌ ❌
assembly)

Non-derived class
(different ✔️ ❌ ❌ ❌ ❌ ❌
assembly)
 In C#, a constructor is similar to a method that is
invoked when an object of the class is created.
 However, unlike methods, a constructor:
 has the same name as that of the class
 does not have any return type

C# class Car
{
Constructors // constructor
Car()
{
//code
}

}
Car car1 = new Car();
Types of Constructors
 Parameterless Constructor
 When we create a constructor without parameters, it is
known as a parameterless constructor.
 Parameterized Constructor
 In C#, a constructor can also accept parameters. It is
C# called a parameterized constructor
Constructors  Default Constructor
 If we have not defined a constructor in our class, then the
C# will automatically create a default constructor with an
empty code and no parameters
using System;
namespace Constructor
{
class Car
{

// parameterless constructor
Car()
{
Console.WriteLine("Parameterless Constructor");
Parameterless }

Constructor static void Main(string[] args)


{

// call constructor
new Car();
Console.ReadLine();

}
}
} Output: Parameterless Constructor
namespace Constructor
{
class Car
{
string brand;
int price;
// parameterized constructor
Car(string theBrand, int thePrice)
{

Parameterized brand = theBrand;


price = thePrice;
Constructor }
static void Main(string[] args)
{
// call parameterized constructor
Car car1 = new Car("MU", 50000);

Console.WriteLine("Brand: " + car1.brand);


Console.WriteLine("Price: " + car1.price);
Console.ReadLine();
}
Brand: MU
}
Price: 50000
}
namespace Constructor
{

class Program
{

int a;

static void Main(string[] args)


{
Default // call default constructor
Constructor Program p1 = new Program();
Console.WriteLine("Default value of a: " + p1.a);
Console.ReadLine();

}
}
}
Default value of a: 0
 A copy constructor is used to create an object by copying data
from another object
namespace Constructor
{
class Car
{
string brand;
Car(string theBrand)
{
brand = theBrand;
}
Copy // copy constructor
Car(Car c1)
Constructor {
brand = c1.brand;
}
static void Main(string[] args)
{
// call constructor
Car car1 = new Car("BMW");
Console.WriteLine("Brand of car1: " + car1.brand);
// call the copy constructor
Car car2 = new Car(car1);
Console.WriteLine("Brand of car2: " + car2.brand);
Console.ReadLine(); Brand of car1: BMW
} } } Brand of car2: BMW
 A private constructor can be created using the private
access specifier. This is known as a private constructor
Private in C#.
Constructor
 Once the constructor is declared private, we cannot
create objects of the class in other classes.
 The static constructor is called only once during the execution of
the program.
 We can have only one static constructor in a class.
 It cannot have any parameters or access modifiers.
namespace Constructor
{
class Car
{ // static constructor
static Car()
Static {
Console.WriteLine("Static Constructor");
Constructor }
// parameterless constructor
Car()
{
Console.WriteLine("Default Constructor");
}
static void Main(string[] args)
{
// call parameterless constructor
Car car1 = new Car();
// call parameterless constructor again Static Constructor
Car car2 = new Car(); Default Constructor
Console.ReadLine(); } } } Default Constructor
 Data abstraction is the process of hiding certain details and
showing only essential information to the user.
 Abstraction can be achieved with either abstract classes or
interfaces.
 Abstract class cannot be instantiated.
Abstract methods have the following features:
 An abstract method is implicitly a virtual method.
Abstract Class  Abstract method declarations are only permitted in abstract
classes.
 Because an abstract method declaration provides no actual
implementation, there is no method body; the method
declaration simply ends with a semicolon and there are no curly
braces ({ }) following the signature.
 For example:
 public abstract void Semester();
// Abstract class
abstract class MU
{ public int a = 10;
// Abstract method (does not have a body)
public abstract void Semester();
// Regular method
public void sleep()
{
Console.WriteLine("Zzz");
}}

Abstract Class class UG : MU


{
public override void Semester()
{ Console.WriteLine("I am from UG");
} }

class Program{
static void Main(string[] args)
{ MU m = new UG();
m.Semester();
m.sleep(); I am from UG
}} Zzz
 In C#, an interface is similar to abstract class. However, unlike
abstract classes, all methods of an interface are fully abstract
(method without body).
 We use the interface keyword to create an interface.
 We cannot use access modifiers inside an interface.
 All members of an interface are public by default.
Interface  An interface doesn't allow fields.
 Syntax:

interface InterfaceName
{
// method without body
void calculateArea();
}
// Interface
interface MU
{
//int a = 10;
void Semester();
}
interface Results
{
void Grade();
}
class Student : MU, Results
{
public void Semester(){
Interface }
Console.WriteLine("Exam");

public void Grade()


{
Console.WriteLine("Grade");
}
}
class Program{
static void Main(string[] args)
{
Student s = new Student();
s.Semester();
s.Grade(); Exam
} } Grade
Parameters Interface Abstract class
Speed Slow Fast
Multiple Inheritances Implement several Interfaces Only one abstract class
Abstract & concrete
Structure Abstract methods
methods
When to use Future enhancement To avoid independence
Interface vs Inheritance/ A Class can implement The class can inherit only
Abstract Class Implementation multiple interfaces one Abstract Class
Hiding 100% abstraction 0 to 100% abstraction
Instantiation Object cannot be created Object cannot be created
Methods in interface are abstract Both abstract & non
Methods by default & compulsory to abstract methods can be
override. used.
Override keyword is
Overriding No extra keyword
used.
 In C#, inheritance allows us to create a new class from an existing
class. It is a key feature of Object-Oriented Programming (OOP).
 The class from which a new class is created is known as the base class
(parent or superclass). And, the new class is called derived class (child
or subclass).
 The derived class inherits the fields and methods of the base class.
This helps with the code reusability in C#.
 In C#, inheritance is an is-a relationship. We use inheritance only if
Inheritance there is an is-a relationship between two classes.
 Types:
 Single
 Multilevel
 Multiple (Interfaces)
 Hierarchical
 Hybrid
using System;
public class Employee
{
public float salary = 40000;
}
public class Programmer : Employee
{
public float bonus = 10000;
Single }
class TestInheritance
Inheritance {
public static void Main(string[] args)
{
Programmer p1 = new Programmer();
Console.WriteLine("Salary: " + p1.salary);
Console.WriteLine("Bonus: " + p1.bonus);
}
} Salary: 40000
Bonus: 10000
using System;
public class Animal
{
public void eat() { Console.WriteLine("Eat"); }
}
public class Dog : Animal
{
public void bark() { Console.WriteLine("Bark"); }
}
public class BabyDog : Dog
{
Multilevel public void weep() { Console.WriteLine("Weep"); }
}
Inheritance class TestInheritance2
{
public static void Main(string[] args)
{
BabyDog d1 = new BabyDog();
d1.eat();
d1.bark();
d1.weep(); Eat
} Bark
} Weep
Multiple  Interface
Inheritance
using System;
public class Animal
{
public void eat() { Console.WriteLine(“Animal"); }
}
public class Dog : Animal
{
public void eat() { Console.WriteLine("Dog"); }
}
public class BabyDog : Animal
{
Hierarchical public void eat() { Console.WriteLine("BabyDog"); }
}
Inheritance class TestInheritance2
{
public static void Main(string[] args)
{
BabyDog d1 = new BabyDog();
d1.eat();
d1.bark();
d1.weep(); Animal
} Dog
} BabyDog
class UGC
{
public void Ugc()
{
Console.WriteLine("UGC Approved"); } }
// Interface
interface MU
{
//int a = 10;
void Semester(); }
interface Results
{
void Grade(); }

Hybrid class Aicte: UGC


{
public void AICTE()
Inheritance {
Console.WriteLine("AICTE Approved");
} }
class Student : Aicte, MU, Results{
public void Semester()
{ Console.WriteLine("Exam");}
public void Grade()
{ Console.WriteLine("Grade");} }
class Program{
static void Main(string[] args){ UGC Approved
Student s = new Student(); AICTE Approved
s.Ugc();
s.AICTE();
Exam
s.Semester(); Grade
s.Grade(); } }
• In C#, base keyword is used to access fields, constructors
and methods of base class.
Base Keyword • You can use base keyword within instance method,
constructor or instance property accessor only. You can't
use it inside the static method.
using System;
namespace Inheritance{ // base class
class A
{
public virtual void e()
{ Console.WriteLine("A"); } }

class D : A
{
public override void e()
{
Base Keyword base.e();
inside method Console.WriteLine("D"); }
class Program
}

{
static void Main(string[] args)
{

D d = new D();
d.e();
}
}
}
using System;
public class Animal {
public string color = "white";
}
public class Dog : Animal {
string color = "black";
Base Keyword public void showColor() {
to refer Console.WriteLine(base.color);
variable Console.WriteLine(color);
} }
public class TestBase {
public static void Main() {
Dog d = new Dog();
white
d.showColor();
black
} }
using System;
public class Animal {
public Animal()
{
Console.WriteLine("animal"); } }
public class Dog : Animal {
public Dog() {
Base Class Console.WriteLine("dog"); } }
Constructor public class TestOverriding
{
public static void Main()
{
Dog d = new Dog();
animal
}
dog
}
 Polymorphism simply means occurring in more than
one form.
 That is, the same entity (method or operator or object)
can perform different operations in different scenarios.
Polymorphism
 There are two types of polymorphism:
 Compile Time Polymorphism / Static Polymorphism
 Run-Time Polymorphism / Dynamic Polymorphism
 In compile time polymorphism, the compiler identifies
which method is being called at the compile time.
Compile Time  In C#, we achieve compile time polymorphism through
2 ways:
Polymorphism
 Method overloading
 Operator overloading
using System;
class Program
{
// method adds two integer numbers
void totalSum(int a, int b)
{
Console.WriteLine("The sum of numbers is " + (a + b));
}

// method adds two double-type numbers


Method // totalSum() method is overloaded
void totalSum(double a, double b)
Overloading {
Console.WriteLine("The sum of numbers is " + (a + b));
}
static void Main(string[] args)
{
Program sum1 = new Program();
sum1.totalSum(5, 7);
sum1.totalSum(53.5, 8.7);
}
} The sum of numbers is 12
The sum of numbers is 62.2
int x = 7;
int y = 5;
C# Operator int sum = x + y;
Overloading Console.WriteLine(sum);
// Output: 12
using System;
namespace Inheritance
{ // base class
class A
{
public virtual void e()
{ Console.WriteLine("A "); } }

class D : A
Method {
public override void e()
Overriding {
Console.WriteLine(" D");
} }
class Program{
static void Main(string[] args)
{
A a = new A();
a.e();
D d = new D();
d.e(); A
D
} } }
using System;
namespace SealedClass
{
sealed class Animal
{
}

// Error Code
class Dog : Animal
{
Sealed }

Keyword class Program


{
static void Main(string[] args)
{
// create an object of Dog class
Dog d1 = new Dog();
Console.ReadLine();
} }
}
error CS0509: 'Dog': cannot derive from sealed type
'Animal'
 In C#, the exception handling method is implemented
using the try catch and finally statement.
 The try encloses the code that might throw an
exception, whereas the catch handles an exception if
one exists.
 The finally is used for any cleanup work that needs to
Exception be done.
Handling
 System Exception are Built-in Exception
 Checked and
 Unchecked Exceptions
Exception
 Checked occurs at Compile Time – Ex DivideByZero
Handling
 Unchecked occurs at Run Time – Ex
ArrayIndexOutOfBound
//C#: Exception Handling
using System;
class MyClient{
public static void Main(){
int x = 10;
int div = 0;
try
{
Exception div = 100 / x;
Console.WriteLine("This linein not executed");
}
catch (DivideByZeroException)
{
Console.WriteLine("Exception occured");
}
Console.WriteLine($"Result is {div}");
} }
Activity  https://www.menti.com/alhmvt7gkwmz
 Introduction to .NET Framework Architecture
 CLR
 Assemblies
 Basics of C#
 Class
 Object
 Method
Summary
 Access Modifiers
 Constructors
 Abstract Class
 Inheritance
 Interface
 Polymorphism
 Exception Handling
 ASP.Net Web Application:
 Page life cycle of ASP.NET Application
 Web Controls (Button, TextBox, CheckBox, Image etc.)
 Rich Controls (Calendar, AdRotator)
Up Next  Validation Controls
 State management
 Cookie
 Session

You might also like