Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Hands-On Object-Oriented Programming with C#

You're reading from   Hands-On Object-Oriented Programming with C# Build maintainable software with reusable code using C#

Arrow left icon
Product type Paperback
Published in Feb 2019
Publisher Packt
ISBN-13 9781788296229
Length 288 pages
Edition 1st Edition
Languages
Arrow right icon
Authors (2):
Arrow left icon
Abhishek Sur Abhishek Sur
Author Profile Icon Abhishek Sur
Abhishek Sur
Raihan Taher Raihan Taher
Author Profile Icon Raihan Taher
Raihan Taher
Arrow right icon
View More author details
Toc

Table of Contents (16) Chapters Close

Preface 1. Overview of C# as a Language FREE CHAPTER 2. Hello OOP - Classes and Objects 3. Implementation of OOP in C# 4. Object Collaboration 5. Exception Handling 6. Events and Delegates 7. Generics in C# 8. Modeling and Designing Software 9. Visual Studio and Associated Tools 10. Exploring ADO.NET with Examples 11. New Features in C# 8 12. Understanding Design Patterns and Principles 13. Git - The Version Control System 14. Prepare Yourself - Interviews and the Future 15. Other Books You May Enjoy

Method group conversion

In the last example, we saw how we can create an object of a delegate and pass the method name in the constructor. Now we will look at another way of achieving the same thing, but in an easier way. This is called method group conversion. Here, you don't need to initialize the delegate object, but you can directly assign the method to it. Let me show you an example:

using System;

namespace Delegate1
{
delegate int MathFunc(int a, int b);

class Program
{
static void Main(string[] args)
{
MathFunc mf = add;

Console.WriteLine("add");
Console.WriteLine(mf(4, 5));

mf = sub;

Console.WriteLine("sub");
Console.WriteLine(mf(4, 5));
Console.ReadKey();
}

public static int add(int a, int b)
{
return a + b;
}

public static int sub(int a, int b)
{
return (a > b) ? (a - b) : (b - a);
}
}
}

Here, we can see that instead of passing the method name...

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime