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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T17:14:23+00:00 2026-05-22T17:14:23+00:00

I am writing integration tests that involve FileSystemWatcher objects. To make things easier, I

  • 0

I am writing integration tests that involve FileSystemWatcher objects. To make things easier, I want to unsubscribe everything from an event delegate without having to hunt down every subscription. I already saw related post, Is it necessary to unsubscribe from events?. This is somewhat a duplicate, but I am specifically asking why this doesn’t work with a FileSystemWatcher object.

It would be nice to do something like the following:

private void MethodName()
{
    var watcher = new FileSystemWatcher(@"C:\Temp");
    watcher.Changed += new FileSystemEventHandler(watcher_Changed);

    watcher.Changed = null; // A simple solution that smells of C++.

    // A very C#-ish solution:
    foreach (FileSystemEventHandler eventDelegate in 
             watcher.Changed.GetInvocationList())
        watcher.Changed -= eventDelegate;
}

No matter how the Changed event is referenced, the compiler reports:
The event ‘System.IO.FileSystemWatcher.Changed’ can only appear on the left hand side of += or -=

The above code works just fine, when working with an event in the same class:

public event FileSystemEventHandler MyFileSystemEvent;

private void MethodName()
{
    MyFileSystemEvent += new FileSystemEventHandler(watcher_Changed);

    MyFileSystemEvent = null; // This works.

    // This works, too.
    foreach (FileSystemEventHandler eventDelegate in 
             MyFileSystemEvent.GetInvocationList())
        watcher.Changed -= eventDelegate;
}

So, what am I missing? It seems that I should be able to do the same with the FileSystemWatcher events.

  • 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-22T17:14:24+00:00Added an answer on May 22, 2026 at 5:14 pm

    When you declare event in your class, it is an equivalent (almost) of the following code:

    private FileSystemEventHandler _eventBackingField;
    public event FileSystemEventHandler MyFileSystemEvent
    {
        add
        {
            _eventBackingField =
                (FileSystemEventHandler)Delegate.Combine(_eventBackingField, value);
        }
        remove
        {
            _eventBackingField =
                (FileSystemEventHandler)Delegate.Remove(_eventBackingField, value);
        }
    }
    

    Notice that there is no set or get accessor for event (like for properties) and you can’t explicitly write them.

    When you write MyFileSystemEvent = null in your class, it is actually doing _eventBackingField = null, but outside your class there is no way to directly set this variable, you have only event add & remove accessors.

    This might be a confusing behavior, because inside your class you can reference an event handler delegate by event name, and can’t do that outside the class.

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

Sidebar

Related Questions

As part of a suite of integration tests I am writing, I want to
I am writing a bunch of integration tests for a project. I want to
I'm writing a tool to run a series of integration tests on my product.
Is it common to write integration tests before writing unit tests? Is it conventional,
I'm writing an integration tests for my database, and I've got one question. At
I'm writing some integrations tests in JUnit. What happens here is that when i
I am writing an application that uses 3rd party libraries to instantiate and make
I'm writing unit tests (technically integration tests since I am connecting to a database)
I'm writing some integration tests using a robot. I have the robot opening a
I'm writing some integration test with plone.app.testing. Sometimes I want to print something to

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.