"Delegate subtraction has unpredictable result" in ReSharper/C#?

  • or, can I just ignore ReSharper?
  • Ok I got a different approach than others:

    Here is simplified code:

    public delegate void MyHandler (object sender);
    
    
    MyHandler _myEvent;
    
    
    public event MyHandler MyEvent
    {
    add
    {
    _myEvent += value;
    DoSomethingElse();
    }
    remove
    {
    _myEvent -= value; // <-- ReSharper warning here
    }
    }
    

    Firstly, I assumed few things before hand.

    gitude; var div = document.getElementById("location");

    Person who is listing an event has smartphone(if it is a browser, I dont have to make these assumptions) with:

      div.innerHTML = "You are at Latitude: " + latitude + ", Longitude: " + longitude;
    1. GPS

    2. HTML5 capability.

    3. }
    4. Javascript capability

    How should I save the time stamps in the database?

    Step 2. Give the (Long,Lat) as arguements to some of the (Lat,Long) to TimeZone api's like the Yahoo API (use Flag R to get the Latitude converted to Timezone) to get the users timezone.

    Solution: Obviously UTC, I overlaid procedure below:

    => The users timezone is determined without user input ( I am using this because you cannot simply assume that user knows the timezone of place he lives in, I knew my timezone only after few months or something at the place :P, pretty dumb!)

    The each Event table has Timezone, Event & so can also have CityName Then create another database table with classification based on CityNames. So here the user will have two columns

    |---------------------------------------|
    |_____NewYork________|______Sydney______|
    |                    |                  |
    |Event 1             |  Event 2         |
    |____________________|__________________|
    

    Step 1. Use the Geolocation of the user using the Geolocation API

        window.onload = getMyLocation;
    
    
    function getMyLocation() {
    if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(displayLocation);
    } else {
    alert("Oops, no geolocation support");
    }
    }
    
    
    function displayLocation(position) {
    var latitude = position.coords.latitude;
    var longitude = position.coords.longitude;
    var div = document.getElementById("location");
    div.innerHTML = "You are at Latitude: " + latitude + ", Longitude: " + longitude;
    }
    

    For UI

    Step 2. Give the (Long,Lat) as arguements to some of the (Lat,Long) to TimeZone api's like the Yahoo API (use Flag R to get the Latitude converted to Timezone) to get the users timezone.

    => use Google Calendar API or some of the Calendar API's

    => The users timezone is determined without user input ( I am using this because you cannot simply assume that user knows the timezone of place he lives in, I knew my timezone only after few months or something at the place :P, pretty dumb!)

    The each Event table has Timezone, Event & so can also have CityName Then create another database table with classification based on CityNames. So here the user will have two columns

    |---------------------------------------|
    |_____NewYork________|______Sydney______|
    |                    |                  |
    |Event 1             |  Event 2         |
    |____________________|__________________|
    

    For UI

    => use Google Calendar API or some of the Calendar API's

    Related Readings:

      Related Readings:

      1. Determine timezone from latitude/longitude without using web services like Geonames.org

      2. Determine timezone from latitude/longitude without using web services like Geonames.org

      3. Timezone lookup from latitude longitude

      I know its just about showing some idea how to solve this problem. But see how accurate & light-weight it becomes on users when the Timezone is determined by using device APIs

      Hope it helps!

    33263 次浏览

    Don't be afraid! The first part of ReSharper's warning only applies to removing lists of delegates. In your code, you're always removing a single delegate. The second part talks about ordering of delegates after a duplicate delegate was removed. An event doesn't guarantee an order of execution for its subscribers, so it doesn't really affect you either.

    Since the above mechanics can lead to unpredictable results, ReSharper issues a warning whenever it encounters a delegate subtraction operator.

    ReSharper is issuing this warning because multicast delegate subtraction can have gotchas, it isn't condemning that language feature entirely. Luckily those gotchas are in fringe cases and you are unlikely to encounter them if you're just instrumenting simple events. There is no better way to implement your own add/remove handlers, you just gotta take notice.

    I'd suggest downgrading ReSharper's warning level for that message to "Hint" so that you don't get desensitized to their warnings, which are usually useful.

    set it to = null instead of using -=

    When assigning Method3, I completely lost the two initial subscriptions. Event usage will avoid this problem and at same time remove the ReSharper warning.