Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7892051
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T06:46:21+00:00 2026-06-03T06:46:21+00:00

I have a delegate with a generic type as one of the parameters: public

  • 0

I have a delegate with a generic type as one of the parameters:

public delegate void UpdatedPropertyDelegate<T>(
    RemoteClient callingClient, 
    ReplicableProperty<T> updatedProp, 
    ReplicableObject relevantObject
);

Now, I want a public event that can be subscribed to for other classes to use. Therefore, I did:

public event UpdatedPropertyDelegate<T> UpdatedProperty;

However, the compiler doesn’t like that. I don’t understand why T has to be specified here. Surely it’s specified when I fire the event, i.e.:

if (UpdatedProperty != null) 
{
    UpdatedProperty(this, readProperty, 
        ReplicableObjectBin.GetObjectByID(readProperty.OwnerID));
}

So, am I doing something simple wrong? Or is this a massive failure of understanding?

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T06:46:22+00:00Added an answer on June 3, 2026 at 6:46 am

    It sounds like what you need is an interface type, rather than a delegate. Interface methods can accept open generic types (which is what you’re after), even though delegates cannot. For example, one could define something like:

    interface ActOnConstrainedThing<CT1,CT2>
    {
      void Act<MainType>(MainType param) where MainType: CT1,CT2;
    }
    

    Even if implementers of CT1 and CT2 do not share a common base type which also implements CT1 and CT2, an implementation of Act may use its passed-in parameter as a CT1 or CT2 without typecast, and could even pass it to routines which expect a generic parameter with CT1 and CT2 constraints. Such a thing would not be possible with delegates.

    Note that using interfaces rather than delegates means that one cannot use the normal “event” mechanism and syntax. Instead, the object which would be an event publisher must maintain a list of object instances that implement the desired interface (e.g. a List<ActOnConstrainedThing<IThis,IThat>>) , and enumerate the instances on that list (perhaps using foreeach). For example:

    List<IActOnConstrainedThing<IThis,IThat>> _ActOnThingSubscribers;
    
    void ActOnThings<T>(T param) where T:IThis,IThat
    {
      foreach(var thing in _ActOnThingSubscribers)
      {
        thing.Act<T>(param);
      }
    }
    
    

    Edit/Addendum

    The place where I employed this pattern also had some other stuff that didn’t seem overly relevant to the question, which by my interpretation was asking how one can have a delegate (or equivalent) with an open type parameter, so that the object invoking the delegate-equivalent can supply the type parameter, without the object supplying the delegate having to know it in advance. Most cases where this is useful involve generic constraints, but since that was apparently introducing confusion, here’s an example that doesn’t:

    interface IShuffleFiveThings
    {
      void Shuffle<T>(ref T p1, ref T p2, ref T p3, ref T p4, ref T p5);
    }
    List<IShuffleFiveThings _ShuffleSubscribers;
    
    void ApplyShuffles<T>(ref T p1, ref T p2, ref T p3, ref T p4, ref T p5)
    {
      foreach(var shuffler in _ShuffleSubscribers)
      {
        thing.Shuffle(ref p1, ref p2, ref p3, ref p4, ref p5);
      }
    }
    

    The IShuffleFiveThings.Shuffle<T> method takes five parameters by ref and does something with them (most likely permutes them in some fashion; perhaps permuting them randomly, or perhaps permuting some randomly while leaving others where they are. If one has a list IShuffleFiveThings, the things in that list can be used efficiently, without boxing or Reflection, to manipulate any kind of thing (including both class types and value types). By contrast, if one were to use delegates:

    delegate void ActOn5RefParameters(ref p1, ref p2, ref p3, ref p4, ref p5);
    

    then because any particular delegate instance can only act upon a single parameter type supplied at its creation (unless it’s an open delegate which is called only via Reflection), one would need to create a separate list of delegates for every type of object one wished to shuffle (yes, I know one would normally handle permutations by using an array of integer indices; I chose permutation as an operation because it’s applicable to all object types, not because this particular method of permuting things is useful).

    Note that because the type T in IShuffleFiveThings does not have any constraints, implementations won’t be able to do much with it except by typecasting (which may introduce boxing). Adding constraints to such parameters makes them much more useful. While it would be possible to hard-code such constraints within the interface, that would limit the interface’s usefulness to applications requiring those particular constraints. Making the constraints themselves generic avoids that restriction.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When i have delegate like public delegate void PrintMe(); (1) PrintMe a = delegate()
I have a simple generic delegate: delegate void CommandFinishedCallback<TCommand>(TCommand command) where TCommand : CommandBase;
I have a method that given a delegate Type argument (not a generic), it
I have this delegate in C#. public delegate string ACSharpDelegate(string message); How would I
let's say I have public delegate DataSet AutoCompleteDelegate( string filter, long rowOffset); can I
following setup, i have several generic functions, and i need to choose the type
.NET 2.0 added the EventHandler<TArgs> generic delegate type to simplify the process of writing
I have an interface method public void Execute(ICommand command); which needs to pass known
class Test { public delegate void FruitDelegate(Fruit f); public void Notify<T>(Action<T> del) where T
Can we have same delegate for two events ? which have same number of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.