OOP2 Lecture Week 08(Spring2023 24)
OOP2 Lecture Week 08(Spring2023 24)
Delegates
Course Code: CSC 2210 Course Title: Object Oriented Programming 2
return isOpen;
}
Interface
Any class or struct that implements the IEquatable<T> interface must contain a definition
for an Equals method that matches the signature that the interface specifies. As a result,
you can count on a class that implements IEquatable<T> to contain an Equals method
with which an instance of the class can determine whether it's equal to another instance
of the same class.
// Implementation of IEquatable<T>
interface
public bool Equals(Car car)
{
return (this.Make, this.Model, this.Year) ==
(car.Make, car.Model, car.Year);
}
}
Interface
Why and When to use
To achieve security - hide certain details and only show the important details of an
object (interface).
C# does not support "multiple inheritance" (a class can only inherit from one base
class). However, it can be achieved with interfaces, because the class can implement
multiple interfaces.
To implement multiple interfaces, separate them with a comma.
Inherited class must implement all members of interface otherwise it will become
abstract also.
Structure can also implement interfaces.
• As we know you can’t create or call constructor of interface but by implementing the
interface one can create interface type variable.
• Only members from individual interface can be called using the variable.
• Consider the following code snippet where Test class implements ITest1 & ITest2
interface containing Method1() and Method2()
class Program
{
static void Main()
{
var stringCollection = new
SampleCollection<string>();
stringCollection[0] = "Hello, World";
Console.WriteLine(stringCollection[0]);
}
}
// The example displays the following output:
Indexer
Indexer Overload
Property Indexer
Allows methods to be called as if they were public data Allows elements of an internal collection of an object to be
members. accessed by using array notation on the object itself.
A get accessor of a property has no parameters. A get accessor of an indexer has the same formal parameter
list as the indexer.
A set accessor of a property contains the A set accessor of an indexer has the same formal parameter
implicit value parameter. list as the indexer, and also to the value parameter.
Supports shortened syntax with Auto-Implemented Supports expression bodied members for get only indexers.
Properties.
Delegate
Delegate
A delegate is an object which refers to a method or you can say it is a reference type
variable that can hold a reference to the methods.
A delegate is a type that represents references to methods with a particular parameter
list and return type.
Delegates are used to pass methods as arguments to other methods.
When we instantiate a delegate, we can associate its instance with any method with a
compatible signature and return type.
One can invoke (or call) the method through the delegate instance.
public delegate int PerformCalculation(int x, int y);
public static void DelegateMethod(string message){
Console.WriteLine(message);
}
Any method from any accessible class or struct that matches the delegate type can be
assigned to the delegate.
The method can be either static or an instance method.
This makes it possible to programmatically change method calls, and also plug new
code into existing classes.
Delegates are similar to C++ function pointers, but delegates are fully object-oriented,
and unlike C++ pointers to member functions, delegates encapsulate both an object
instance and a method.
Delegates can be used to define callback methods.
Delegates allow methods to be passed as parameters.
Event handlers are nothing more than methods that are invoked through delegates.
Delegate
Delegate types are derived from the Delegate class in the .NET Framework.
Delegate Type is also Reference type.
Action and Func are delegates that we can use instead of defining our own
delegate types. Important to remember: Action and Func are delegates.
Func<> to represent a method that returns something.
Action and Action<> represent methods that return nothing.
Delegate
Delegate Multicasting
Delegates can be chained together; for example, multiple methods can be called on a
single event.
When a delegate is wrapped with more than one method that is known as a multicast
delegate.
Multicast delegates are used extensively in event handling.
The "+" or "+=" operator adds a function to the invocation list, and the "-" and "-="
operator removes it.
Delegate
public delegate void MyDelegate(string msg); //declaring a
delegate
class Program
{
static void Main(string[] args)
{
MyDelegate del1 = ClassA.MethodA;
MyDelegate del2 = ClassB.MethodB;
You can create a delegate, but there is no need to declare the method associated with it.
You do not have to explicitly define a method prior to using the delegate. Such a method
is referred to as anonymous. In other words, if a delegate itself contains its method
definition it is known as anonymous method.
The code on is an example of an anonymous method:
print(100);
}
using System;
Display();
return 0;
}
}
Delegate
Anonymous Method
• C# 4.0 The Complete Reference; Herbert Schildt; McGraw-Hill Osborne Media; 2010
• Head First C# by Andrew Stellman
• Fundamentals of Computer Programming with CSharp – Nakov v2013
References
• https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/
interface
• https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/interfaces/
• https://www.c-sharpcorner.com/UploadFile/sekarbalag/Interface-In-CSharp/
• https://www.geeksforgeeks.org/c-sharp-interface/
• https://www.tutorialsteacher.com/csharp/csharp-interface
• https://www.tutorialspoint.com/csharp/csharp_interfaces.htm
• https://www.w3schools.com/cs/cs_interface.asp
• https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/indexers/
• https://www.tutorialsteacher.com/csharp/csharp-indexer
• https://www.infoworld.com/article/3018437/how-to-work-with-indexers-in-c.html
• https://www.c-sharpcorner.com/article/indexer-in-C-Sharp/
• https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/delegates/
• https://www.c-sharpcorner.com/UploadFile/puranindia/C-Sharp-net-delegates-
and-events/
• https://www.pluralsight.com/guides/how-why-to-use-delegates-csharp
References
• https://www.tutorialsteacher.com/csharp/csharp-delegates
• https://www.tutorialspoint.com/csharp/csharp_delegates.htm
• https://www.geeksforgeeks.org/c-sharp-delegates/
• https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/delegates/
using-delegates
• https://www.tutorialsteacher.com/csharp/csharp-anonymous-method
• https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/statements-
expressions-operators/anonymous-functions