---
title: "Representing void methods with generic delegates"
url: "https://osherove.com/blog/2005/10/27/representing-void-methods-with-generic-delegates.md"
canonical: "https://osherove.com/blog/2005/10/27/representing-void-methods-with-generic-delegates.html"
published: "2005-10-27T14:20:00"
updated: "2005-10-27T14:20:00"
tags: [".Net 2.0"]
author: "Roy Osherove"
description: "Generics are cool. Learn about them . I want to talk about a specific query that I had which right now I can only guess the answer. To begin, here's the scenario. You can create a Generic Delegate to point to a method…"
usage: "Licensed under CC BY 4.0 — reuse, quote and adapt with attribution (Roy Osherove, link to the original page)."
license: "https://creativecommons.org/licenses/by/4.0/"
---

# Representing void methods with generic delegates

By Roy Osherove October 27, 2005 In [.Net 2.0](https://osherove.com/blog/category/net-2-0)

**Historical article** · Originally published October 27, 2005; technical details may be outdated.

Generics are cool. [Learn about them](http://msdn2.microsoft.com/en-us/library/0x6a29h6.aspx).

I want to talk about a specific query that I had which right now I can only guess the answer.

To begin, here's the scenario.

You can create a Generic Delegate to point to a method you want to execute. For example this simple method :

**void myMethod(int number)**

you could create a generic delegate:

**delegate void invokeDelegate<T>(T input)**

can invoke it like this: **invokeDelegate<int> invoker = myMethod;**

_(I'm using the new c# 2.0 syntax that allows me to create an "inferred delegate" instead of writing "**new invokeDelegate<int>(myMethod)**" )_

it even cool if the method had a return type like this:

**bool myMethod(int number)**

you could create a generic delegate:

**delegate RET invokeDelegate<T,RET>(T input)**

and invoke it thusly:

**invokeDelegate<int,bool> invoker = myMethod;**

but how do you take the delegate I've just written and apply it to the \*first\* method which returns a void? Turns out you can't, at least from my poking around. The more I thought about it the more it makes sense.

If you allow a "void" type to be used as generic type parameter, it is implied that this parameter can be used not only as a return value but as a parameter to the generic function/delegate itself. Here's what someone \*could\* write if this was possible:

**delegate void invokeDelegate<T,RET>(T input,RET moreinput)**

assuming someone does this (if the compiler allowed it):

**invokeDelegate<int,typeof(void)> invoker = myMethod;**

it would have to implicitly mean that there's a method somewhere looking like this:

**void myMethod(int number,void IhaveNoIdeaWhatThisIs)**

which means, in essence, that to represent all method signatures you'd always need at least two generic delegates, one void and one with a generic return value.

That's just my line of thinking, but I'd love for someone from MS to comment on this.

## More in .Net 2.0

[All posts in .Net 2.0](https://osherove.com/blog/category/net-2-0)

- Oct 30, 2005

    ### [Amazoner 1.0.1 - Larger Images for Books and a dedicated website](https://osherove.com/blog/2005/10/30/amazoner-101-larger-images-for-books-and-a-dedicated-website.html)

- Oct 30, 2005

    ### [Strongly Typed Reflection Calls in .NET 2.0](https://osherove.com/blog/2005/10/30/strongly-typed-reflection-calls-in-net-20.html)

- Oct 21, 2005

    ### [Nice Guidance Automation kit hands on lab from TechEd (with a fix for a build problem it contains)](https://osherove.com/blog/2005/10/21/nice-guidance-automation-kit-hands-on-lab-from-teched-with-a.html)

- Oct 19, 2005

    ### [Early experiences from .NET 2.0 Winforms](https://osherove.com/blog/2005/10/19/early-experiences-from-net-20-winforms.html)


---

## About the author

**Roy Osherove**: Senior Solutions Architect at AWS working on AI for production and AI-driven development; author of The Art of Unit Testing and Elastic Leadership, with about 20 years of consulting and training on unit testing, TDD, team leadership and continuous delivery.

### Sites

- [RobotPaper](https://robotpaper.ai/): This blog: AI patterns and anti-patterns, AI and coding agents, and engineering leadership.
- [osherove.com](https://osherove.com): Roy's personal site. Author of The Art of Unit Testing and Elastic Leadership; consulting and training on unit testing, TDD, team leadership and continuous delivery, plus about 20 years of blog posts.
- [The Art of Unit Testing](https://www.artofunittesting.com): The Art of Unit Testing, 3rd Edition (Manning): readable, maintainable, trustworthy unit tests, plus TDD workshops in C#, JavaScript, Java and Kotlin.
- [5whys.com](https://www.5whys.com): Roy's writing on software team leadership: self-organizing teams, surviving chaos, learning mode and the Elastic Leadership model.
- [Pipeline Driven](https://pipelinedriven.org): Continuous delivery and DevOps practices: build patterns, pipeline-friendly testing, DevOps metrics and enterprise DevOps.
- [Elastic Leadership](https://www.elasticleadership.com): Elastic Leadership, Roy's framework for software team leaders (survival, learning and self-organizing modes).

### Social

- X: https://x.com/royosherove
- LinkedIn: https://www.linkedin.com/in/osherove/

### Contact

- Contact form: https://forms.gle/jQSx5KoXvrVA1JCK7

### License

Licensed under CC BY 4.0 — reuse, quote and adapt with attribution (Roy Osherove, link to the original page). License: https://creativecommons.org/licenses/by/4.0/
