Repository: AvaloniaUI/Avalonia
File: src/Avalonia.Styling/Styling/Styles.cs (C#)

Code before:
    public class Styles : AvaloniaObject, IAvaloniaList<IStyle>, IStyle, ISetResourceParent
    {
        private IResourceNode _parent;
        private IResourceDictionary _resources;
        private AvaloniaList<IStyle> _styles = new AvaloniaList<IStyle>();
        private Dictionary<Type, List<IStyle>> _cache;
        private bool _notifyingResourcesChanged;

        public Styles()
        {
            _styles.ResetBehavior = ResetBehavior.Remove;
            _styles.ForEachItem(
                x =>
                {
                    if (x.ResourceParent == null && x is ISetResourceParent setParent)
                    {
                        setParent.SetParent(this);
                        setParent.ParentResourcesChanged(new ResourcesChangedEventArgs());
                    }

                    if (x.HasResources)
                    {
                        ResourcesChanged?.Invoke(this, new ResourcesChangedEventArgs());
                    }

                    x.ResourcesChanged += NotifyResourcesChanged;
                    _cache = null;
                },
                x =>
                {
                    if (x.ResourceParent == this && x is ISetResourceParent setParent)
                    {
                        setParent.SetParent(null);
                        setParent.ParentResourcesChanged(new ResourcesChangedEventArgs());
                    }

                    if (x.HasResources)
                    {
                        ResourcesChanged?.Invoke(this, new ResourcesChangedEventArgs());
                    }

                    x.ResourcesChanged -= NotifyResourcesChanged;
                    _cache = null;
                },
                () => { });
        }

        public event NotifyCollectionChangedEventHandler CollectionChanged
        {
            add => _styles.CollectionChanged += value;
            remove => _styles.CollectionChanged -= value;

Review comment (suggestion):
Not something that we want to fix within this PR but I wanted to ask if we have any other alternatives to using events for this? We create so many delegates for this that in calendar benchmark it was like the 6th top allocation.

![image](https://user-images.githubusercontent.com/2588062/75100500-7c953a80-55ce-11ea-9d7d-b360fc71e7ac.png)

![image](https://user-images.githubusercontent.com/2588062/75100506-9cc4f980-55ce-11ea-955a-d2c6538f524c.png)

Code after:
    public class Styles : AvaloniaObject, IAvaloniaList<IStyle>, IStyle, ISetResourceParent
    {
        private IResourceNode _parent;
        private IResourceDictionary _resources;
        private AvaloniaList<IStyle> _styles = new AvaloniaList<IStyle>();
        private Dictionary<Type, List<IStyle>> _cache;
        private bool _notifyingResourcesChanged;

        public Styles()
        {
            _styles.ResetBehavior = ResetBehavior.Remove;
            _styles.ForEachItem(
                x =>
                {
                    if (x.ResourceParent == null && x is ISetResourceParent setParent)
                    {
                        setParent.SetParent(this);
                        setParent.ParentResourcesChanged(new ResourcesChangedEventArgs());
                    }

                    if (x.HasResources)
                    {
                        ResourcesChanged?.Invoke(this, new ResourcesChangedEventArgs());
                    }

                    x.ResourcesChanged += NotifyResourcesChanged;
                    _cache = null;
                },
                x =>
                {
                    if (x.ResourceParent == this && x is ISetResourceParent setParent)
                    {
                        setParent.SetParent(null);
                        setParent.ParentResourcesChanged(new ResourcesChangedEventArgs());
                    }

                    if (x.HasResources)
                    {
                        ResourcesChanged?.Invoke(this, new ResourcesChangedEventArgs());
                    }

                    x.ResourcesChanged -= NotifyResourcesChanged;
                    _cache = null;
                },
                () => { });
        }

        public Styles(IResourceNode parent)
            : this()
        {
            _parent = parent;