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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T06:37:41+00:00 2026-06-17T06:37:41+00:00

I often get some resharper warnings regarding unassigned objects created with ‘new’ expression. Following

  • 0

I often get some resharper warnings regarding “unassigned objects created with ‘new’ expression”. Following code snipped shall demonstrate the situation:

I am using a helper class(Observer.cs) which monitors some properties from an other class (MonitoredClass.cs). When a property changes, the observer class writes the changed value into a other data class (DataClass.cs).

simplified code snipped:

MonitoredClass.cs:

public class MonitoredClass : INotifyPropertyChanged
{
   // simplified: in fact property calls OnPropertyChange(..)
   public string Property1 { get; set; }
}

DataClass.cs:

public class DataClass
{
   public string LastProperty1Value { get; set; }
}

Observer.cs:

public class Observer
{
   private MonitoredClass _monitoredClass;
   private DataClass _dataClass;
   public Observer(MonitoredClass monitoredClass, DataClass dataClass)
   {
      _monitoredClass = monitoredClass;
      _dataClass = dataClass;
      _monitoredClass.PropertyChanged+=MonitoredClassPropertyChanged;
   }

   private void MonitoredClassPropertyChanged(..)
   {
      _dataClass.LastProperty1Value = _monitoredClass.Property1;
   }
}

So far so good.

If I use now my Observer class from above as follows:

...
new Observer(monitoredClassInstance, dataClassInstance);
...

than I get a resharper warning “possible unassigned object created by ‘new’ expression”.

My question now is, if there is a better solution/pattern to design this observer.
Of coarse, i can allocate the new observer instance to a private field. But than I have a field which is never used.
Or I can set the monitoredClassInstance and dataClassInstance with properties instead of pass them in the constructor. But this only prevents the warning, but in fact does not change the architecture.

Thanks in advance for your advice, opinion, pattern etc.

  • 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-17T06:37:42+00:00Added an answer on June 17, 2026 at 6:37 am

    It may be fine as it is. Of course, it only works because you have attached an event handler, thereby tying the lifetime of the Observer to that of the MonitoredClass. If you had not attached an event handler, then the Observer would have no references to it and it would (eventually) be garbage collected.

    Thinking about it, it might therefore be clearer to make the constructor private and write a public static factory method to create it:

    public class Observer
    {
        private MonitoredClass _monitoredClass;
        private DataClass _dataClass;
    
        public static void Observe(MonitoredClass monitoredClass, DataClass dataClass)
        {
            new Observer(monitoredClass, dataClass);
        }
    
        private Observer(MonitoredClass monitoredClass, DataClass dataClass)
        {
            _monitoredClass = monitoredClass;
            _dataClass = dataClass;
            _monitoredClass.PropertyChanged+=MonitoredClassPropertyChanged;
        }
    
        private void MonitoredClassPropertyChanged(..)
        {
            _dataClass.LastProperty1Value = _monitoredClass.Property1;
        }
    }
    

    Then you can suppress the warning inside Observe() and people who call it won’t need to worry about it.

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

Sidebar

Related Questions

I have some scripts that get run often, always from within a connected SQLPlus
I get this error in xml file very often. here is the code in
I'm fairly new to Photoshop (Still CS4) and I often get designs for websites
Often, I come across code where the Getter method is repeatedly used/abused to get
I've created a test WP7 app, which consumes a WCF to get some sample
When I compile I often get the following error: Error 28 Error : DEP0500
I am new to Entity Framework and I want to get some points about
I often get to the question when using a service, should I find a
I often get in a position when I need to know why my LINQ
I often get strange resulst when passing int variables to tasks such as in

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.