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 6549127
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:03:18+00:00 2026-05-25T12:03:18+00:00

I’m building in some flexibility to a program, given its purpose has now changed.

  • 0

I’m building in some flexibility to a program, given its purpose has now changed. I want to add in an interface which an existing class will implement.

Existing Class

public class ClassA
{
    #region Events
    public delegate void DataReceivedHandler(object sender, EventArgs e);
    public delegate void DataSentHandler(object sender, EventArgs e);
    public delegate void StatusUpdatedHandler(object sender, EventArgs e);
    public event DataReceivedHandler DataReceived;
    public event DataSentHandler DataSent;
    public event StatusUpdatedHandler StatusUpdated;
    #endregion

    //rest of code here...
 }

New Interface

public interface IClassA
{
    event EventHandler DataReceived;
    event EventHandler DataSent;
    event EventHandler StatusUpdated;

    //rest of code here...
}

My three questions are as follows.

Q1. Am I right in the first place to declare my own delegates in the class i.e. public delegate void DataReceivedHandler, or should these be removed and the events replaced with a normal EventHandler. i.e. public event EventHandler DataReceived. Given that these events aren’t passing data with the event, just notifying anything that has subscribed that something has changed.

Q2. Tied closely to the question above. Given I am changing the properties of the class, i.e. status (StatusUpdated). Is it best practice to pass the new state as a custom EventArgs, thus requiring a custom delegate as in the original code? As it currently works the event is fired, and the subscribed class can just use the sender value, i.e. (sender as ClassA).Status. Is there a best practice or is it just up to the developer?

Q3. Now that is out the way. The part about the interface. Have I correctly declared the DataReceived and other events in the interface, to match the class that will be marked as implementing it.

  • 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-05-25T12:03:19+00:00Added an answer on May 25, 2026 at 12:03 pm

    If you need custom delegates for interface methods, you should declare them separately (outside a class), because they are not tied to a specific implementation. A delegate declaration can sit inside an interface, just like a class does. The issue with your delegates is that they all have the same signature (that of an EventHandler), and are therefore redundant.

    If you don’t need to pass any data when raising those events, you can use a plain EventHandler delegate. However, I would expect to get some data with a DataReceived event, so you should check if that makes sense and possibly use a delegate which accepts some data as a parameter.

    Third, your interface event signatures must match class signatures. You cannot use an EventHandler delegate type in an interface, and change the signature inside the class.

    Note that you can also avoid declaring delegate types explicitly, and used generic Action delegates provided with the BCL (starting with in .NET 3.5). In that case you might have something like:

    public interface IClassA
    {
       event Action<IClassA, Data> DataReceived;
       event Action<IClassA, Data> DataSent;
       event Action<IClassA, Status> StatusUpdated;
    }
    

    The latter approach gives you the benefit of having a strongly typed sender parameter (opposed to an object in EventHandler).

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

Sidebar

Related Questions

I want to count how many characters a certain string has in PHP, but
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I have a jquery bug and I've been looking for hours now, I can't
Basically, what I'm trying to create is a page of div tags, each has

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.