IntoTheBlue
IntoTheBlue

Reputation: 199

What is the difference between calling Dispose or setting to "default" in Xamarin.Android?

I've inherited code which sets view elements to default in OnDestroy() rather than call Dispose() in all Fragments. What is the difference between/impact of these approaches?

e.g.

    public override void OnDestroy()
    {
        ClearViewElements();

        base.OnDestroy();
    }

    private void ClearViewElements()
    {
        _aRecyclerView.RemoveAllViewsInLayout();
        _aRecyclerView.SetAdapter(null);
        if (_aListAdapter != null)
        {
            _aListAdapter.DeleteClick = default;
            _aListAdapter = default;
        }
        _aRecyclerView = default;
        _aButton = default;
        _anEditText = default;
        _aLayout = default;
    }

Upvotes: 0

Views: 80

Answers (1)

Ivan I
Ivan I

Reputation: 9990

The default will just set the value to the object default, which should be null.

Assuming it is null, this has similarities with Dispose, but it is not the same. As the object shouldn't have any reference any more (with some caveats like if all subscriptions are cleared) it is available for the garbage collection and it should be eventually collected. However with Dispose you mark that the object should be collected, so the app is aware of that from that moment, not from the moment when it checks for the free references.

Upvotes: 2

Related Questions