@@ -40,7 +40,7 @@ public partial struct ImmutableArray<T> : IReadOnlyList<T>, IList<T>, IEquatable
40
40
public static readonly ImmutableArray < T > Empty = new ImmutableArray < T > ( new T [ 0 ] ) ;
41
41
42
42
/// <summary>
43
- /// The backing field for this instance. References to this value should never be shared with outside code.
43
+ /// The backing field for this instance. References to this value should never be shared with outside code.
44
44
/// </summary>
45
45
/// <remarks>
46
46
/// This would be private, but we make it internal so that our own extension methods can access it.
@@ -583,11 +583,11 @@ public ImmutableArray<T> InsertRange(int index, ImmutableArray<T> items)
583
583
584
584
if ( self . IsEmpty )
585
585
{
586
- return new ImmutableArray < T > ( items . array ) ;
586
+ return items ;
587
587
}
588
588
else if ( items . IsEmpty )
589
589
{
590
- return new ImmutableArray < T > ( self . array ) ;
590
+ return self ;
591
591
}
592
592
593
593
return self . InsertRange ( index , items . array ) ;
@@ -636,7 +636,7 @@ public ImmutableArray<T> AddRange(ImmutableArray<T> items)
636
636
if ( self . IsEmpty )
637
637
{
638
638
// Be sure what we return is marked as initialized.
639
- return new ImmutableArray < T > ( items . array ) ;
639
+ return items ;
640
640
}
641
641
else if ( items . IsEmpty )
642
642
{
@@ -728,7 +728,7 @@ public ImmutableArray<T> Remove(T item, IEqualityComparer<T> equalityComparer)
728
728
self . ThrowNullRefIfNotInitialized ( ) ;
729
729
int index = self . IndexOf ( item , equalityComparer ) ;
730
730
return index < 0
731
- ? new ImmutableArray < T > ( self . array )
731
+ ? self
732
732
: self . RemoveAt ( index ) ;
733
733
}
734
734
@@ -876,7 +876,7 @@ public ImmutableArray<T> RemoveAll(Predicate<T> match)
876
876
877
877
if ( self . IsEmpty )
878
878
{
879
- return new ImmutableArray < T > ( self . array ) ;
879
+ return self ;
880
880
}
881
881
882
882
List < int > removeIndexes = null ;
@@ -942,14 +942,14 @@ public ImmutableArray<T> Sort(int index, int count, IComparer<T> comparer)
942
942
Requires . Range ( index >= 0 , "index" ) ;
943
943
Requires . Range ( count >= 0 && index + count <= self . Length , "count" ) ;
944
944
945
- if ( comparer == null )
946
- {
947
- comparer = Comparer < T > . Default ;
948
- }
949
-
950
945
// 0 and 1 element arrays don't need to be sorted.
951
946
if ( count > 1 )
952
947
{
948
+ if ( comparer == null )
949
+ {
950
+ comparer = Comparer < T > . Default ;
951
+ }
952
+
953
953
// Avoid copying the entire array when the array is already sorted.
954
954
bool outOfOrder = false ;
955
955
for ( int i = index + 1 ; i < index + count ; i ++ )
@@ -970,7 +970,7 @@ public ImmutableArray<T> Sort(int index, int count, IComparer<T> comparer)
970
970
}
971
971
}
972
972
973
- return new ImmutableArray < T > ( self . array ) ;
973
+ return self ;
974
974
}
975
975
976
976
/// <summary>
@@ -1007,7 +1007,7 @@ public Enumerator GetEnumerator()
1007
1007
/// Returns a hash code for this instance.
1008
1008
/// </summary>
1009
1009
/// <returns>
1010
- /// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.
1010
+ /// A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.
1011
1011
/// </returns>
1012
1012
[ Pure ]
1013
1013
public override int GetHashCode ( )
@@ -1592,16 +1592,16 @@ internal void ThrowNullRefIfNotInitialized()
1592
1592
{
1593
1593
// Force NullReferenceException if array is null by touching its Length.
1594
1594
// This way of checking has a nice property of requiring very little code
1595
- // and not having any conditions/branches.
1595
+ // and not having any conditions/branches.
1596
1596
// In a faulting scenario we are relying on hardware to generate the fault.
1597
- // And in the non-faulting scenario (most common) the check is virtually free since
1597
+ // And in the non-faulting scenario (most common) the check is virtually free since
1598
1598
// if we are going to do anything with the array, we will need Length anyways
1599
- // so touching it, and potentially causing a cache miss, is not going to be an
1599
+ // so touching it, and potentially causing a cache miss, is not going to be an
1600
1600
// extra expense.
1601
1601
var unused = this . array . Length ;
1602
1602
1603
1603
// This line is a workaround for a bug in C# compiler
1604
- // The code in this line will not be emitted, but it will prevent incorrect
1604
+ // The code in this line will not be emitted, but it will prevent incorrect
1605
1605
// optimizing away of "Length" call above in Release builds.
1606
1606
// TODO: remove the workaround when building with Roslyn which does not have this bug.
1607
1607
var unused2 = unused ;
@@ -1612,7 +1612,7 @@ internal void ThrowNullRefIfNotInitialized()
1612
1612
/// <see cref="IsDefault"/> property returns true. The
1613
1613
/// <see cref="InvalidOperationException"/> message specifies that the operation cannot be performed
1614
1614
/// on a default instance of <see cref="ImmutableArray{T}"/>.
1615
- ///
1615
+ ///
1616
1616
/// This is intended for explicitly implemented interface method and property implementations.
1617
1617
/// </summary>
1618
1618
private void ThrowInvalidOperationIfNotInitialized ( )
@@ -1642,7 +1642,7 @@ private ImmutableArray<T> RemoveAtRange(ICollection<int> indexesToRemove)
1642
1642
if ( indexesToRemove . Count == 0 )
1643
1643
{
1644
1644
// Be sure to return a !IsDefault instance.
1645
- return new ImmutableArray < T > ( self . array ) ;
1645
+ return self ;
1646
1646
}
1647
1647
1648
1648
var newArray = new T [ self . Length - indexesToRemove . Count ] ;
0 commit comments