From 0b187df8cb1b00f246d048d4502bc3147607be2f Mon Sep 17 00:00:00 2001 From: Jon Hanna Date: Fri, 6 Jan 2017 19:13:04 +0000 Subject: [PATCH] Null-coalesce within enumerable types. (#1136) Reduces field-access. --- Src/Newtonsoft.Json/Linq/JEnumerable.cs | 7 +---- .../Utilities/CollectionWrapper.cs | 28 ++----------------- 2 files changed, 4 insertions(+), 31 deletions(-) diff --git a/Src/Newtonsoft.Json/Linq/JEnumerable.cs b/Src/Newtonsoft.Json/Linq/JEnumerable.cs index a1cb1f7e..193ebad0 100644 --- a/Src/Newtonsoft.Json/Linq/JEnumerable.cs +++ b/Src/Newtonsoft.Json/Linq/JEnumerable.cs @@ -67,12 +67,7 @@ namespace Newtonsoft.Json.Linq /// public IEnumerator GetEnumerator() { - if (_enumerable == null) - { - return Empty.GetEnumerator(); - } - - return _enumerable.GetEnumerator(); + return (_enumerable ?? Empty).GetEnumerator(); } IEnumerator IEnumerable.GetEnumerator() diff --git a/Src/Newtonsoft.Json/Utilities/CollectionWrapper.cs b/Src/Newtonsoft.Json/Utilities/CollectionWrapper.cs index 52f8ce6c..80204c31 100644 --- a/Src/Newtonsoft.Json/Utilities/CollectionWrapper.cs +++ b/Src/Newtonsoft.Json/Utilities/CollectionWrapper.cs @@ -169,24 +169,12 @@ namespace Newtonsoft.Json.Utilities public virtual IEnumerator GetEnumerator() { - if (_genericCollection != null) - { - return _genericCollection.GetEnumerator(); - } - - return _list.Cast().GetEnumerator(); + return (_genericCollection ?? _list.Cast()).GetEnumerator(); } IEnumerator IEnumerable.GetEnumerator() { - if (_genericCollection != null) - { - return _genericCollection.GetEnumerator(); - } - else - { - return _list.GetEnumerator(); - } + return ((IEnumerable)_genericCollection ?? _list).GetEnumerator(); } int IList.Add(object value) @@ -333,17 +321,7 @@ namespace Newtonsoft.Json.Utilities public object UnderlyingCollection { - get - { - if (_genericCollection != null) - { - return _genericCollection; - } - else - { - return _list; - } - } + get { return (object)_genericCollection ?? _list; } } } } \ No newline at end of file