Open Delegates vs. Closed Delegates

.Net supports two kinds of delegates: Open delegates and closed delegates.

When you create a delegate that points to an instance method, the instance that you created it from is stored in the delegate’s Target property.  This property is passed as the first parameter to the method that the delegate points to.  For instance methods, this is the implicit this parameter; for static methods, it's the method's first parameter.  These are called closed delegates, because they close over the first parameter and bring it into the delegate instance.

It is also possible to create open delegates which do not pass a first parameter.  Open delegates do not use the Target property; instead, all of the target method’s parameters are passed from the delegate’s formal parameter list, including the first parameter.  Therefore, an open delegate pointing to a given method must have one parameter more than a closed delegate pointing to the same method.  Open delegates are usually used to point to static methods.  When you make a delegate pointing to a static method, you (generally) don't want the delegate to hold a first parameter for the method. 

In addition to these two normal cases, it is also possible (in .Net 2.0 and later) to create open delegates for instance methods and to create closed delegates for static methods.  With one exception, C# doesn’t have any syntactical support for these unusual delegates, so they can only be created by calling the CreateDelegate method.

Open delegates by calling the CreateDelegate overload that doesn’t take a target parameter.  Before .Net 2.0, this function could only be called with a static method.   In .Net 2.0, you can call this function with an instance method to create an open delegate.  Such a delegate will call use its first parameter as this instead of its Target field. 

As a concrete example, consider the String.ToUpperInvariant() method.  Ordinarily, this method takes no parameters, and operates on the string it’s called on.  An open delegate pointing to this instance method would take a single string parameter, and call the method on that parameter.

For example:

Func<string> closed = new Func<string>("a".ToUpperInvariant);
Func<string, string> open = (Func<string, string>)
        typeof(Func<string, string>),

closed();     //Returns "A"
open("abc");  //Returns "ABC"

Closed delegates are created by calling the CreateDelegate overload that takes a target parameter.  In .Net 2.0, this can be called with a static method and an instance of that method’s first argument type to create a closed delegate that calls the method with the given target as its first parameter.  Closed delegates curry the first parameter from the target method.  For example:

Func<object, bool> deepThought = (Func<object, bool>)
        typeof(Func<object, bool>),
        typeof(object).GetMethod("Equals", BindingFlags.Static | BindingFlags.Public)

This code curries the static Object.Equals method to create a delegate that calls Equals with 2 and the delegate’s single parameter).  It’s equivalent to x => Object.Equals(2, x).  Note that since the method is (generally) not a member of the target object’s type, we need to pass an actual MethodInfo instance; a name alone isn’t good enough.

Note that you cannot create a closed delegate from a static method whose first parameter is a value type, because, unlike all instance methods, static delegates that take value types receive their parameters by value, not as a reference.   For more details, see here and here.

C# 3 added limited syntactical support for creating closed delegates.  You can create a delegate from an extension method as if it were an instance method on the type it extends.  For example:

var allNumbers = Enumerable.Range(1, Int32.MaxValue);
Func<int, IEnumerable<int>> countTo = allNumbers.Take;

This code creates an IEnumerable<int> containing all positive integers, then creates a closed delegate that curries this sequence into the static Enumerable.Take<T>(IEnumerable<T>) method.

Except for extension methods, open instance delegates and closed static delegates are rarely used in actual code.  However, it is important to understand how ordinary open and closed delegates work, and where the target object ends up for instance delegates.


This information you give us is great. Monica

The above article is nice and interesting, thank you willing to share! Greetings success of admin Percetakan Murah Rawamangun Jakarta Timur wish you deign to visit my website, thank you :)

I advise you to read this info if you are going to write your literature review. It's important to know

How to write my annotated bibliography is the most frequent question that students ask. However, if you are interested in this information, contact the writing service.

Visit and make sure that it is easy to place an order here. If you carefully read all the information about this writing company and are ready to place an order here, do not hesitate to log in to website.

This Slack Blog is now writing on programming languages and web development. Still, my profession is separate. I'm a beautician and refer to the Krave beauty discount code to the modern generation of beauticians. In the beautician industry, these products have accuracy and worth it.

You need to submit an essay soon and you haven't started writing anything yet? Don't know which topic is better to write about? I think you should come here us history regents essay and pass your work perfectly !!

My Assignment Expert is now one of the leading companies in the market which providesSQL Assignments
to students in their computer programming assignments. Our service care support is available 24/7 round the clock.

It is wise to use this move.
It should definitely work, I am constantly learning new information, I like to learn what is very far from.
I write articles, poems, write term papers on various topics.
We have extended essay writers on the service who will help everyone to write the necessary written work.
We work at the highest level and are responsible for quality

In 2021, academic help is a service that most US students seek most often. We understand that essay writing pay 4 essay brings less and less value to a generation that communicates via TikTok, Instagram Direct, and Twitter.

A tax advisor is a financial professional with tax-specific expertise. These advisors, sometimes referred to as tax consultants. The Tax Advisor the magazine of planning, trends, and techniques reports and explains federal tax issues to tax practitioners.

Thanks for sharing this post. I got information on this post. Keep sharing.
Lexington Reckless Driving

What's more, the main way they would is to wear something of extraordinary quality with a message all the while. BARRACUDA QUEENS JACKETS COLLECTION

bancruptcy lawyer near me
"The debate between open delegates and closed delegates is a fascinating exploration of democracy and representation. Open delegates emphasize inclusivity and broad participation, while closed delegates prioritize party unity and cohesion. Both approaches have their merits and challenges, making it a thought-provoking discussion in the world of politics."

Thank you to the creator of this insightful blog for shedding light on the transformative potential of blockchain technology in healthcare. Your efforts to educate and inform are greatly appreciated, and your contributions are instrumental in advancing the conversation around innovation in the healthcare sector.blockchain technology in healthcare Keep up the excellent work!

"Thank you to the creator of this insightful blog for sharing valuable information and knowledge. Your efforts in compiling such helpful content are truly appreciated. branding agency in chennai, It's a great resource for anyone seeking to learn and stay updated. Keep up the fantastic work!"

Thank you to the blog creator for crafting such an insightful and engaging piece. Your dedication to sharing valuable information and enriching content is truly appreciated. balaji tanjore painting, Keep up the excellent work, and may your contributions continue to inspire and educate readers around the world.

Function pointers serve as powerful tools in programming, allowing functions to be treated as variables. They enable dynamic selection and invocation of functions at runtime, enhancing flexibility and modularity in code. By passing function pointers as arguments or storing them in data structures, developers can implement advanced algorithms and design patterns with ease.
virginia beach uncontested divorce manual

🙏 Thank you for sharing your blog post! Your words have the power to inspire, educate, and connect with readers. best architects in chennai
Keep writing and sharing your unique perspective with the world.

Thank you for your comment! Your insights are valuable and greatly appreciated. It's always encouraging to see thoughtful engagement from readers like you. ai marketing company, Looking forward to hearing more of your thoughts in future discussions!

Post a Comment