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

  • Home
  • SEARCH
  • 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 529651
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T09:06:51+00:00 2026-05-13T09:06:51+00:00

If I have ClassA that has a public event, SomeEvent, and ClassC that has

  • 0

If I have ClassA that has a public event, SomeEvent, and ClassC that has method, addListener, that accepts an EventHandler reference, why can’t ClassB have a line that says c.addListener(ref a.SomeEvent)? If I try I get a compiler error that says: “The event ‘ClassA.SomeEvent’ can only appear on the left hand side of += or -= (except when used from within the type ‘ClassA’).

Why does this restriction exist? And how can I get around it while staying reasonably close to my structure?

I’m a C# newbie; any help would be appreciated. Thanks!

class ClassA {
    public event EventHandler SomeEvent;
}

ClassB{
    public ClassB() {

        ClassA a = new ClassA();
        ClassC c = new ClassC();
        c.addListener(ref a.SomeEvent);  //Compile error
    }
}

class ClassC {
    public void addListener(ref EventHandler handler) {
        handler += onEvent;
    }

    private void onEvent(object sender, EventArgs e) {
        //do stuff
    }

}
  • 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-13T09:06:51+00:00Added an answer on May 13, 2026 at 9:06 am

    The event keyword creates an accessor for a private delegate object. The exact same thing a property does, it restricts access to a private field. Your code snippet fails with a similar kind of error when you use a property instead of an event:

      class ClassA {
        public int Property { get; set; }
      }
      class ClassB {
        public ClassB() {
          ClassA a = new ClassA();
          ClassC c = new ClassC();
          c.setValue(ref a.Property);   // CS0206
        }
      }
      class ClassC {
        public void setValue(ref int value) {
          value = 42;
        }
      }
    

    It is easier to see now, there is no way for the compiler to ensure that the setValue() method uses the property setter. Nor could it know that the “value” argument is a property with a setter or a plain field.

    It is less clear for an event because there is so much syntax sugar at work. This declaration

    public event EventHandler SomeEvent;
    

    actually generates this code:

    private EventHandler _SomeEvent;
    public event SomeEvent {
      add    { _SomeEvent += new EventHandler(value); }
      remove { _SomeEvent -= new EventHandler(value); }
    }
    

    The add and remove accessors are equivalent to the get and set accessors of a property, they prevent code from messing with the private _SomeEvent field. By convention, the add accessor is invoked when you use +=, remove is invoked with -=. Compare this with the earlier example I gave for a property. Same problem, you can’t use the ref keyword and ClassC.addListener() would have no way to know that the handler is actually an event instead of a delegate object. If the compiler would pass _SomeEvent instead, the point of using the accessors is lost.

    You can restructure the code to solve this problem:

      class ClassC {
        public EventHandler getListener() {
          return new EventHandler(onEvent);
        }
        private void onEvent(object sender, EventArgs e) { }
      }
    ...
          a.SomeEvent += c.getListener();
    

    One final note: the symmetry between an event and a property is a bit lost, the C# compiler automatically generates the add/remove accessors if you don’t write them explicitly. It doesn’t do this for a property. It would have made automatic properties a lot easier:

    property int Property;
    

    But that would have required adding a new keyword to the language, something the C# team really dislikes. Other languages like VB.NET and C++/CLI do have that keyword.

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

Sidebar

Related Questions

You have a method on a class that has 2 parameters, one of which
I have a class that has two method in it, one calls a class
I have a class that has a Generic type G In my class model
I have a class that has some properties. And I want something that calculates
I have a class that has a vector of another class objects as a
Let's say I have a class that has a member called data which is
I have a base class that has a private static member: class Base {
I have an entity class that has a property with an underlying db column
Let's say I have one class Foo that has a bunch of logic in
I have a page base class that has no .aspx file and so I

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.