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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T21:31:37+00:00 2026-05-29T21:31:37+00:00

Lets say I have the below code. What is the difference between assigning the

  • 0

Lets say I have the below code. What is the difference between assigning the actions directly and subscribing to an event?

//Action directly assigned
public class ClassA
{
  public Action<string> OnAdd;

  private void SomethingHappened()
  {
     OnAdd("It Happened");
  }
}

public class ClassB
{

  public ClassB()
  {
    var myClass = new ClassA();
    myClass.OnAdd =  Add;
  }

  private void Add(string Input)
  {
    //do something
  }  
}

//Event handlers
public class ClassA
{
  public event Action<string> OnAdd;

  private void SomethingHappened()
  {
    if (OnAdd != null)
     OnAdd("It Happened"); //Should it be OnAdd.Invoke("It Happened") ???????
  }
}

public class ClassB
{

  public ClassB()
  {
    var myClass = new ClassA();
    myClass.OnAdd += Add;
  }

  private void Add(string Input)
  {
    //do something
  }  
}
  • 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-29T21:31:39+00:00Added an answer on May 29, 2026 at 9:31 pm

    (As an aside, it’s hard to explain things when you’ve used the same type names twice.)

    When you use a public field, clients can not only subscribe to events – they can also completely remove other event handlers by assigning instead of adding:

    myClass.OnAdd = Add;
    

    They can also invoke the handler directly:

    myClass.OnAdd("foo");
    

    Both of these violate the normal pub/sub pattern, where the various subscribers are isolated from one another. Subscribers don’t get to overwrite each other’s subscriptions (only add or remove their own) and they don’t get to raise the event themselves.

    For more on events and delegates, see my article on the topic.

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

Sidebar

Related Questions

Lets say I have the following code snippet below, how do I also apply
For the moment lets say I have plotted a LineChart with the below code.
lets say we have the code below: <html> <head> </head> <body> <p id=paragraph> <h1>HELLO</h1>
I have a basic PHP question, take the code below for example, let's say
Lets say I have model inheritance set up in the way defined below. class
Lets say have this immutable record type: public class Record { public Record(int x,
Lets say I have the following code: abstract class Animal case class Dog(name:String) extends
Right now, lets say I have code much like this... $some_var=returnsUserInput(); function funcA($a) {...}
Lets say I have the following code: <html> <body> <header> <h1> Hello World </h1>
Lets say we have the following code: #include <iostream> #include <string> struct A {

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.