SlideShare uma empresa Scribd logo
1 de 18
Get set property
class Thought
{
private int a;
public int xy {
get {
return a;
}
set {
a = value;
}
}
}

class Program
{
static void Main( string[] args ){
Thought t = new Thought();
t.xy = 23;
Console.WriteLine("value=“+t.xy);
Console.ReadLine();
}
}
Abstract Class
Motivation: if you think no object of a class should be
created, declare the class abstract
In general, an abstract class is a placeholder in a class
hierarchy that represents a generic concept
The use of abstract classes is a design decision; it helps us
establish common elements in a class that is too general to
instantiate

Vehicle

Car

Boat

Plane
2
Abstract Classes: Syntax
Use the modifier abstract on a class header to declare an
abstract class, e.g.,
abstract class Vehicle
{
// …
public abstract void Move();
}
class Car : Vehicle
{ //…
public void Move()
{
Console.WriteLine( “Car is moving!” );
}
}
abstract class StaffMember
{
// …
public abstract double Pay();
}
3
Abstract Class: Some Properties
An abstract class cannot be instantiated
but you can define a reference of an abstract class

Only abstract classes can contain abstract
methods
The child of an abstract class must override
the abstract methods of the parent, or it too
must be declared abstract
4
C# Contains Many Other Features to
Further Refine the Definition of a Class
Sealed class or method
if you declare a class or method sealed, then the
class or method cannot be extended

Operator overloading
you can define operators for a class so that
operations look more like a normal arithmetic
operation

5
Using Interface for Multiple Inheritance
Java/C# decision: single inheritance,
meaning that a derived class can have only
one parent class
To take the advantages of multiple
inheritance, Java/C# defines interfaces,
which give us the best aspects of multiple
inheritance without the complexity

6
C# Interface
A C# interface is a collection of methods, and properties
it can actually include other types of definitions

These methods and properties have no implementation
An interface is used to formally define a set of methods that a
class will implement
An interface captures one aspect of a class

If class D is derived from class B, then you should feel comfortable in
saying an object of D is also a B
If class D implements interface I, then you should feel comfortable in
saying an object of D has I perspective

7
Interfaces: Syntax
interface is a reserved word
public interface IComplexity
{
int
Level { get; set; }
}
public interface IDisplayAble
{
void Paint();
}
// inherits Question, implements two interfaces
class MultiChoice: Question, IComplexity, IDisplayAble
{
public void Paint() {...}
public int Level {...}
public string GetQuestionPart() {}
public string GetAnswerPart() {}
}

8
Interfaces
Methods in an interface have public visibility by default
An interface cannot be instantiated
A class implements an interface by
stating so in the class header after :

A class can implement multiple interfaces: the interfaces
are separated by commas
If a class asserts that it implements an interface, it must
define all methods in the interface or the compiler will
produce errors
A class that implements an interface can implement
other methods as well
9
Polymorphism via Interfaces
An interface name can be used as the type of
an object reference variable
IDoable obj;

The obj reference can be used to point to
any object of any class that implements the
IDoable interface
The version of doThis that the following line
invokes depends on the type of object that
obj is referring to:
obj.doThis();
10
An Example
ISpeak guest;
guest = new Professor();
guest.Speak();
guest = Dog();
guest.Speak();
ISpeak special;
special = new Professor();
special.Pontificate(); // compiler error

ISpeak special;
special = new Professor();
((Professor)special).Pontificate();

public interface ISpeak
{
public void Speak();
}
class Faculty {…}
class Professor: Faculty, ISpeak
{
//
public void Speak()
{…}
public void Pontificate()
{…}
}
class Animal {}
class Dog: Animal, ISpeak
{
//
public void Speak()
{
…
}
}
11
Exception Handing
Exception handling is an in built mechanism
in .NET framework to detect and handle run
time errors.
The .NET framework contains lots of standard
exceptions.
The exceptions are anomalies that occur
during the execution of a program.
They can be because of user, logic or system
errors.
12
Exception Handing
If a user (programmer) do not provide a
mechanism to handle these anomalies, the
.NET run time environment provide a default
mechanism, which terminates the program
execution.  

13
Syntax
C# provides three keywords try, catch and
finally to do exception handling.
try
{
// Statement which can cause an exception.
}
catch(Type x)
{
// Statements for handling the exception
}
finally
{
//Any cleanup code
} 

14
Uncaught Exception
using System;
class MyClient
{
public static void Main()
{
int x = 0;
int div = 100/x;
Console.WriteLine(div);
}
} 
15
Exception handled
class MyClient
{
public static void Main()
{
int x = 0;
int div = 0;
try
{
div = 100/x;
Console.WriteLine("This line in not executed");
}
catch(DivideByZeroException de){
Console.WriteLine("Exception occured");
}

finally{
Console.WriteLine("Finally Block");
}
}} 

Console.WriteLine("Result is {0}",div);

16
Catching all Exception
using System;
class MyClient
{
public static void Main()
{
int x = 0;
int div = 0;
try
{
div = 100/x;
Console.WriteLine("Not executed line");
}
catch
{
Console.WriteLine("oException" );
}
Console.WriteLine("Result is {0}",div);
}
}
17
Standard Exceptions
System.OutOfMemoryException
System.NullReferenceException
Syste.InvalidCastException
Syste.ArrayTypeMismatchException
System.IndexOutOfRangeException    
    
System.ArithmeticException
System.DevideByZeroException
System.OverFlowException 
18

Mais conteúdo relacionado

Mais procurados

Mais procurados (20)

OOPS IN C++
OOPS IN C++OOPS IN C++
OOPS IN C++
 
Chapter 2 c#
Chapter 2 c#Chapter 2 c#
Chapter 2 c#
 
DITEC - Programming with C#.NET
DITEC - Programming with C#.NETDITEC - Programming with C#.NET
DITEC - Programming with C#.NET
 
Introduction to C++
Introduction to C++Introduction to C++
Introduction to C++
 
Oops lecture 1
Oops lecture 1Oops lecture 1
Oops lecture 1
 
Object oriented programming in C++
Object oriented programming in C++Object oriented programming in C++
Object oriented programming in C++
 
DISE - Windows Based Application Development in C#
DISE - Windows Based Application Development in C#DISE - Windows Based Application Development in C#
DISE - Windows Based Application Development in C#
 
C++ tutorials
C++ tutorialsC++ tutorials
C++ tutorials
 
Inheritance
InheritanceInheritance
Inheritance
 
A COMPLETE FILE FOR C++
A COMPLETE FILE FOR C++A COMPLETE FILE FOR C++
A COMPLETE FILE FOR C++
 
Object-Oriented Programming Using C++
Object-Oriented Programming Using C++Object-Oriented Programming Using C++
Object-Oriented Programming Using C++
 
C++ Inheritance
C++ InheritanceC++ Inheritance
C++ Inheritance
 
C# Summer course - Lecture 3
C# Summer course - Lecture 3C# Summer course - Lecture 3
C# Summer course - Lecture 3
 
class and objects
class and objectsclass and objects
class and objects
 
C# Summer course - Lecture 4
C# Summer course - Lecture 4C# Summer course - Lecture 4
C# Summer course - Lecture 4
 
Constructors and destructors
Constructors and destructorsConstructors and destructors
Constructors and destructors
 
Data types, Variables, Expressions & Arithmetic Operators in java
Data types, Variables, Expressions & Arithmetic Operators in javaData types, Variables, Expressions & Arithmetic Operators in java
Data types, Variables, Expressions & Arithmetic Operators in java
 
02 data types in java
02 data types in java02 data types in java
02 data types in java
 
Class and object
Class and objectClass and object
Class and object
 
C# Summer course - Lecture 2
C# Summer course - Lecture 2C# Summer course - Lecture 2
C# Summer course - Lecture 2
 

Semelhante a Visula C# Programming Lecture 8

C# .NET: Language Features and Creating .NET Projects, Namespaces Classes and...
C# .NET: Language Features and Creating .NET Projects, Namespaces Classes and...C# .NET: Language Features and Creating .NET Projects, Namespaces Classes and...
C# .NET: Language Features and Creating .NET Projects, Namespaces Classes and...yazad dumasia
 
21UCAC31 Java Programming.pdf(MTNC)(BCA)
21UCAC31 Java Programming.pdf(MTNC)(BCA)21UCAC31 Java Programming.pdf(MTNC)(BCA)
21UCAC31 Java Programming.pdf(MTNC)(BCA)ssuser7f90ae
 
Introduction to object oriented programming concepts
Introduction to object oriented programming conceptsIntroduction to object oriented programming concepts
Introduction to object oriented programming conceptsGanesh Karthik
 
Exception handling and packages.pdf
Exception handling and packages.pdfException handling and packages.pdf
Exception handling and packages.pdfKp Sharma
 
oops with java modules i & ii.ppt
oops with java modules i & ii.pptoops with java modules i & ii.ppt
oops with java modules i & ii.pptrani marri
 
06 abstract-classes
06 abstract-classes06 abstract-classes
06 abstract-classesAnup Burange
 
Polymorphism in C# Function overloading in C#
Polymorphism in C# Function overloading in C#Polymorphism in C# Function overloading in C#
Polymorphism in C# Function overloading in C#Abid Kohistani
 
CSharp presentation and software developement
CSharp presentation and software developementCSharp presentation and software developement
CSharp presentation and software developementfrwebhelp
 
Java oops PPT
Java oops PPTJava oops PPT
Java oops PPTkishu0005
 
Chapter27 polymorphism-virtual-function-abstract-class
Chapter27 polymorphism-virtual-function-abstract-classChapter27 polymorphism-virtual-function-abstract-class
Chapter27 polymorphism-virtual-function-abstract-classDeepak Singh
 
classes object fgfhdfgfdgfgfgfgfdoop.pptx
classes object  fgfhdfgfdgfgfgfgfdoop.pptxclasses object  fgfhdfgfdgfgfgfgfdoop.pptx
classes object fgfhdfgfdgfgfgfgfdoop.pptxarjun431527
 
02-OOP with Java.ppt
02-OOP with Java.ppt02-OOP with Java.ppt
02-OOP with Java.pptEmanAsem4
 
Structural pattern 3
Structural pattern 3Structural pattern 3
Structural pattern 3Naga Muruga
 
iOS development introduction
iOS development introduction iOS development introduction
iOS development introduction paramisoft
 

Semelhante a Visula C# Programming Lecture 8 (20)

C# .NET: Language Features and Creating .NET Projects, Namespaces Classes and...
C# .NET: Language Features and Creating .NET Projects, Namespaces Classes and...C# .NET: Language Features and Creating .NET Projects, Namespaces Classes and...
C# .NET: Language Features and Creating .NET Projects, Namespaces Classes and...
 
Interfaces c#
Interfaces c#Interfaces c#
Interfaces c#
 
21UCAC31 Java Programming.pdf(MTNC)(BCA)
21UCAC31 Java Programming.pdf(MTNC)(BCA)21UCAC31 Java Programming.pdf(MTNC)(BCA)
21UCAC31 Java Programming.pdf(MTNC)(BCA)
 
Introduction to object oriented programming concepts
Introduction to object oriented programming conceptsIntroduction to object oriented programming concepts
Introduction to object oriented programming concepts
 
Java interface
Java interfaceJava interface
Java interface
 
Exception handling and packages.pdf
Exception handling and packages.pdfException handling and packages.pdf
Exception handling and packages.pdf
 
06 abstract-classes
06 abstract-classes06 abstract-classes
06 abstract-classes
 
oops with java modules i & ii.ppt
oops with java modules i & ii.pptoops with java modules i & ii.ppt
oops with java modules i & ii.ppt
 
Synapseindia dot net development
Synapseindia dot net developmentSynapseindia dot net development
Synapseindia dot net development
 
06 abstract-classes
06 abstract-classes06 abstract-classes
06 abstract-classes
 
Polymorphism in C# Function overloading in C#
Polymorphism in C# Function overloading in C#Polymorphism in C# Function overloading in C#
Polymorphism in C# Function overloading in C#
 
CSharp presentation and software developement
CSharp presentation and software developementCSharp presentation and software developement
CSharp presentation and software developement
 
Java oops PPT
Java oops PPTJava oops PPT
Java oops PPT
 
Java Interface
Java InterfaceJava Interface
Java Interface
 
Chapter27 polymorphism-virtual-function-abstract-class
Chapter27 polymorphism-virtual-function-abstract-classChapter27 polymorphism-virtual-function-abstract-class
Chapter27 polymorphism-virtual-function-abstract-class
 
C# Unit 2 notes
C# Unit 2 notesC# Unit 2 notes
C# Unit 2 notes
 
classes object fgfhdfgfdgfgfgfgfdoop.pptx
classes object  fgfhdfgfdgfgfgfgfdoop.pptxclasses object  fgfhdfgfdgfgfgfgfdoop.pptx
classes object fgfhdfgfdgfgfgfgfdoop.pptx
 
02-OOP with Java.ppt
02-OOP with Java.ppt02-OOP with Java.ppt
02-OOP with Java.ppt
 
Structural pattern 3
Structural pattern 3Structural pattern 3
Structural pattern 3
 
iOS development introduction
iOS development introduction iOS development introduction
iOS development introduction
 

Último

Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Jisc
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17Celine George
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - Englishneillewis46
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxmarlenawright1
 
Plant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptxPlant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptxUmeshTimilsina1
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxheathfieldcps1
 
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxExploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxPooja Bhuva
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Pooja Bhuva
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfDr Vijay Vishwakarma
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Pooja Bhuva
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...Nguyen Thanh Tu Collection
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsKarakKing
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structuredhanjurrannsibayan2
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and ModificationsMJDuyan
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...pradhanghanshyam7136
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...Nguyen Thanh Tu Collection
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...Poonam Aher Patil
 

Último (20)

Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
Plant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptxPlant propagation: Sexual and Asexual propapagation.pptx
Plant propagation: Sexual and Asexual propapagation.pptx
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxExploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
Beyond_Borders_Understanding_Anime_and_Manga_Fandom_A_Comprehensive_Audience_...
 
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
80 ĐỀ THI THỬ TUYỂN SINH TIẾNG ANH VÀO 10 SỞ GD – ĐT THÀNH PHỐ HỒ CHÍ MINH NĂ...
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 

Visula C# Programming Lecture 8

  • 1. Get set property class Thought { private int a; public int xy { get { return a; } set { a = value; } } } class Program { static void Main( string[] args ){ Thought t = new Thought(); t.xy = 23; Console.WriteLine("value=“+t.xy); Console.ReadLine(); } }
  • 2. Abstract Class Motivation: if you think no object of a class should be created, declare the class abstract In general, an abstract class is a placeholder in a class hierarchy that represents a generic concept The use of abstract classes is a design decision; it helps us establish common elements in a class that is too general to instantiate Vehicle Car Boat Plane 2
  • 3. Abstract Classes: Syntax Use the modifier abstract on a class header to declare an abstract class, e.g., abstract class Vehicle { // … public abstract void Move(); } class Car : Vehicle { //… public void Move() { Console.WriteLine( “Car is moving!” ); } } abstract class StaffMember { // … public abstract double Pay(); } 3
  • 4. Abstract Class: Some Properties An abstract class cannot be instantiated but you can define a reference of an abstract class Only abstract classes can contain abstract methods The child of an abstract class must override the abstract methods of the parent, or it too must be declared abstract 4
  • 5. C# Contains Many Other Features to Further Refine the Definition of a Class Sealed class or method if you declare a class or method sealed, then the class or method cannot be extended Operator overloading you can define operators for a class so that operations look more like a normal arithmetic operation 5
  • 6. Using Interface for Multiple Inheritance Java/C# decision: single inheritance, meaning that a derived class can have only one parent class To take the advantages of multiple inheritance, Java/C# defines interfaces, which give us the best aspects of multiple inheritance without the complexity 6
  • 7. C# Interface A C# interface is a collection of methods, and properties it can actually include other types of definitions These methods and properties have no implementation An interface is used to formally define a set of methods that a class will implement An interface captures one aspect of a class If class D is derived from class B, then you should feel comfortable in saying an object of D is also a B If class D implements interface I, then you should feel comfortable in saying an object of D has I perspective 7
  • 8. Interfaces: Syntax interface is a reserved word public interface IComplexity { int Level { get; set; } } public interface IDisplayAble { void Paint(); } // inherits Question, implements two interfaces class MultiChoice: Question, IComplexity, IDisplayAble { public void Paint() {...} public int Level {...} public string GetQuestionPart() {} public string GetAnswerPart() {} } 8
  • 9. Interfaces Methods in an interface have public visibility by default An interface cannot be instantiated A class implements an interface by stating so in the class header after : A class can implement multiple interfaces: the interfaces are separated by commas If a class asserts that it implements an interface, it must define all methods in the interface or the compiler will produce errors A class that implements an interface can implement other methods as well 9
  • 10. Polymorphism via Interfaces An interface name can be used as the type of an object reference variable IDoable obj; The obj reference can be used to point to any object of any class that implements the IDoable interface The version of doThis that the following line invokes depends on the type of object that obj is referring to: obj.doThis(); 10
  • 11. An Example ISpeak guest; guest = new Professor(); guest.Speak(); guest = Dog(); guest.Speak(); ISpeak special; special = new Professor(); special.Pontificate(); // compiler error ISpeak special; special = new Professor(); ((Professor)special).Pontificate(); public interface ISpeak { public void Speak(); } class Faculty {…} class Professor: Faculty, ISpeak { // public void Speak() {…} public void Pontificate() {…} } class Animal {} class Dog: Animal, ISpeak { // public void Speak() { … } } 11
  • 12. Exception Handing Exception handling is an in built mechanism in .NET framework to detect and handle run time errors. The .NET framework contains lots of standard exceptions. The exceptions are anomalies that occur during the execution of a program. They can be because of user, logic or system errors. 12
  • 13. Exception Handing If a user (programmer) do not provide a mechanism to handle these anomalies, the .NET run time environment provide a default mechanism, which terminates the program execution.   13
  • 14. Syntax C# provides three keywords try, catch and finally to do exception handling. try { // Statement which can cause an exception. } catch(Type x) { // Statements for handling the exception } finally { //Any cleanup code }  14
  • 16. Exception handled class MyClient { public static void Main() { int x = 0; int div = 0; try { div = 100/x; Console.WriteLine("This line in not executed"); } catch(DivideByZeroException de){ Console.WriteLine("Exception occured"); } finally{ Console.WriteLine("Finally Block"); } }}  Console.WriteLine("Result is {0}",div); 16
  • 17. Catching all Exception using System; class MyClient { public static void Main() { int x = 0; int div = 0; try { div = 100/x; Console.WriteLine("Not executed line"); } catch { Console.WriteLine("oException" ); } Console.WriteLine("Result is {0}",div); } } 17