Skip to content

Commit 4be811e

Browse files
authored
Updated Enumerable.Skip<TSource> example for C# and VB (dotnet#8836)
1 parent 6f37562 commit 4be811e

File tree

3 files changed

+51
-55
lines changed

3 files changed

+51
-55
lines changed

snippets/csharp/System.Linq/Enumerable/AggregateTSource/enumerable.cs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2508,23 +2508,20 @@ static void SkipEx1()
25082508
// <Snippet87>
25092509
int[] grades = { 59, 82, 70, 56, 92, 98, 85 };
25102510

2511-
IEnumerable<int> lowerGrades =
2512-
grades.OrderByDescending(g => g).Skip(3);
2513-
2514-
Console.WriteLine("All grades except the top three are:");
2515-
foreach (int grade in lowerGrades)
2511+
Console.WriteLine("All grades except the first three:");
2512+
foreach (int grade in grades.Skip(3))
25162513
{
25172514
Console.WriteLine(grade);
25182515
}
25192516

25202517
/*
25212518
This code produces the following output:
25222519
2523-
All grades except the top three are:
2524-
82
2525-
70
2526-
59
2520+
All grades except the first three:
25272521
56
2522+
92
2523+
98
2524+
85
25282525
*/
25292526
// </Snippet87>
25302527
}

snippets/visualbasic/VS_Snippets_CLR_System/system.Linq.Enumerable/VB/Enumerable.vb

Lines changed: 44 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -649,7 +649,7 @@ Namespace SequenceExamples
649649
New Pet With {.Name = "Whiskers", .Age = 1}})
650650

651651
Dim output1 As New System.Text.StringBuilder
652-
' Enumerate the items in the list, calling DefaultIfEmpty()
652+
' Enumerate the items in the list, calling DefaultIfEmpty()
653653
' with a default value.
654654
For Each pet As Pet In pets1.DefaultIfEmpty(defaultPet)
655655
output1.AppendLine("Name: " & pet.Name)
@@ -662,7 +662,7 @@ Namespace SequenceExamples
662662
Dim pets2 As New List(Of Pet)
663663

664664
Dim output2 As New System.Text.StringBuilder
665-
' Enumerate the items in the list, calling DefaultIfEmpty()
665+
' Enumerate the items in the list, calling DefaultIfEmpty()
666666
' with a default value.
667667
For Each pet As Pet In pets2.DefaultIfEmpty(defaultPet)
668668
output2.AppendLine("Name: " & pet.Name)
@@ -783,7 +783,7 @@ Namespace SequenceExamples
783783
Dim namesList As New List(Of String())(New String()() {names1, names2, names3})
784784

785785
' Select arrays that have four or more elements and union
786-
' them into one collection, using Empty() to generate the
786+
' them into one collection, using Empty() to generate the
787787
' empty collection for the seed value.
788788
Dim allNames As IEnumerable(Of String) =
789789
namesList.Aggregate(Enumerable.Empty(Of String)(),
@@ -970,7 +970,7 @@ Namespace SequenceExamples
970970
New Pet With {.Name = "Whiskers", .Age = 1},
971971
New Pet With {.Name = "Daisy", .Age = 4}})
972972

973-
' Group the pets using Age as the key
973+
' Group the pets using Age as the key
974974
' and selecting only the pet's Name for each value.
975975
Dim query As IEnumerable(Of IGrouping(Of Integer, String)) =
976976
pets.GroupBy(Function(pet) pet.Age,
@@ -1011,7 +1011,7 @@ Namespace SequenceExamples
10111011
New Pet With {.Name = "Whiskers", .Age = 1},
10121012
New Pet With {.Name = "Daisy", .Age = 4}})
10131013

1014-
' Group the pets using Age as the key
1014+
' Group the pets using Age as the key
10151015
' and selecting only the pet's Name for each value.
10161016
' <Snippet122>
10171017
Dim query =
@@ -1024,7 +1024,7 @@ Namespace SequenceExamples
10241024
For Each petGroup In query
10251025
' Print the key value of the IGrouping.
10261026
output.AppendLine(petGroup.Age)
1027-
' Iterate over each value in the IGrouping
1027+
' Iterate over each value in the IGrouping
10281028
' and print the value.
10291029
For Each name As String In petGroup.ageGroup
10301030
output.AppendLine(String.Format(" {0}", name))
@@ -1200,7 +1200,7 @@ Namespace SequenceExamples
12001200
Dim pets As New List(Of Pet)(New Pet() {barley, boots, whiskers, daisy})
12011201

12021202
' Create a collection where each element is an anonymous type
1203-
' that contains a Person's name and a collection of names of
1203+
' that contains a Person's name and a collection of names of
12041204
' the pets that are owned by them.
12051205
Dim query =
12061206
people.GroupJoin(pets,
@@ -1292,7 +1292,7 @@ Namespace SequenceExamples
12921292
Dim pets As New List(Of Pet)(New Pet() {barley, boots, whiskers, daisy})
12931293

12941294
' Create a list of Person-Pet pairs, where each element is an
1295-
' anonymous type that contains a Pet's name and the name of the
1295+
' anonymous type that contains a Pet's name and the name of the
12961296
' Person that owns the Pet.
12971297
Dim query =
12981298
people.Join(pets,
@@ -1526,7 +1526,7 @@ Namespace SequenceExamples
15261526

15271527
NotInheritable Class Max2
15281528
' <Snippet57>
1529-
' This class implements IComparable
1529+
' This class implements IComparable
15301530
' and has a custom 'CompareTo' implementation.
15311531
Class Pet
15321532
Implements IComparable(Of Pet)
@@ -1560,7 +1560,7 @@ Namespace SequenceExamples
15601560
New Pet With {.Name = "Boots", .Age = 4},
15611561
New Pet With {.Name = "Whiskers", .Age = 1}}
15621562

1563-
' Find the "maximum" pet according to the
1563+
' Find the "maximum" pet according to the
15641564
' custom CompareTo() implementation.
15651565
Dim max As Pet = pets.Max()
15661566

@@ -1642,7 +1642,7 @@ Namespace SequenceExamples
16421642

16431643
NotInheritable Class Min9
16441644
' <Snippet67>
1645-
' This class implements IComparable
1645+
' This class implements IComparable
16461646
' and has a custom 'CompareTo' implementation.
16471647
Class Pet
16481648
Implements IComparable(Of Pet)
@@ -1706,7 +1706,7 @@ Namespace SequenceExamples
17061706
New Pet With {.Name = "Boots", .Age = 4},
17071707
New Pet With {.Name = "Whiskers", .Age = 1}}
17081708

1709-
' Find the youngest pet by passing a
1709+
' Find the youngest pet by passing a
17101710
' lambda expression to the Min() method.
17111711
Dim min As Integer = pets.Min(Function(pet) pet.Age)
17121712

@@ -1744,7 +1744,7 @@ Namespace SequenceExamples
17441744
output.AppendLine(fruit)
17451745
Next
17461746

1747-
' The following query shows that the standard query operators such as
1747+
' The following query shows that the standard query operators such as
17481748
' Where() can be applied to the ArrayList type after calling OfType().
17491749
Dim query2 As IEnumerable(Of String) =
17501750
fruits.OfType(Of String)().Where(Function(fruit) _
@@ -1875,13 +1875,13 @@ Namespace SequenceExamples
18751875
' Four
18761876
' four
18771877
' First
1878-
' first
1878+
' first
18791879
' </Snippet140>
18801880
#End Region
18811881

18821882
#Region "OrderByDescending"
18831883
' <Snippet71>
1884-
' This class provides a custom implementation
1884+
' This class provides a custom implementation
18851885
' of the IComparer.Compare() method.
18861886
Class SpecialComparer
18871887
Implements IComparer(Of Decimal)
@@ -1961,7 +1961,7 @@ Namespace SequenceExamples
19611961
#Region "Range"
19621962
Sub RangeEx1()
19631963
' <Snippet72>
1964-
' Generate a sequence of integers from 1 to 10
1964+
' Generate a sequence of integers from 1 to 10
19651965
' and project their squares.
19661966
Dim squares As IEnumerable(Of Integer) =
19671967
Enumerable.Range(1, 10).Select(Function(x) x * x)
@@ -2142,18 +2142,18 @@ Namespace SequenceExamples
21422142
petOwners.SelectMany(Function(petOwner) petOwner.Pets)
21432143

21442144
Dim output As New System.Text.StringBuilder("Using SelectMany():" & vbCrLf)
2145-
' Only one foreach loop is required to iterate through
2145+
' Only one foreach loop is required to iterate through
21462146
' the results because it is a one-dimensional collection.
21472147
For Each pet As String In query1
21482148
output.AppendLine(pet)
21492149
Next
21502150

2151-
' This code demonstrates how to use Select() instead
2151+
' This code demonstrates how to use Select() instead
21522152
' of SelectMany() to get the same result.
21532153
Dim query2 As IEnumerable(Of String()) =
21542154
petOwners.Select(Function(petOwner) petOwner.Pets)
21552155
output.AppendLine(vbCrLf & "Using Select():")
2156-
' Notice that two foreach loops are required to iterate through
2156+
' Notice that two foreach loops are required to iterate through
21572157
' the results because the query returns a collection of arrays.
21582158
For Each petArray() As String In query2
21592159
For Each pet As String In petArray
@@ -2204,8 +2204,8 @@ Namespace SequenceExamples
22042204
New PetOwner With
22052205
{.Name = "Hines, Patrick", .Pets = New String() {"Dusty"}}}
22062206

2207-
' Project the items in the array by appending the index
2208-
' of each PetOwner to each pet's name in that petOwner's
2207+
' Project the items in the array by appending the index
2208+
' of each PetOwner to each pet's name in that petOwner's
22092209
' array of pets.
22102210
Dim query As IEnumerable(Of String) =
22112211
petOwners.SelectMany(Function(petOwner, index) _
@@ -2508,25 +2508,24 @@ Namespace SequenceExamples
25082508

25092509
' Sort the numbers in descending order and
25102510
' get all but the first (largest) three numbers.
2511-
Dim lowerGrades As IEnumerable(Of Integer) =
2511+
Dim skippedGrades As IEnumerable(Of Integer) =
25122512
grades _
2513-
.OrderByDescending(Function(g) g) _
25142513
.Skip(3)
25152514

25162515
' Display the results.
2517-
Dim output As New System.Text.StringBuilder("All grades except the top three are:" & vbCrLf)
2518-
For Each grade As Integer In lowerGrades
2516+
Dim output As New System.Text.StringBuilder("All grades except the first three are:" & vbCrLf)
2517+
For Each grade As Integer In skippedGrades
25192518
output.AppendLine(grade)
25202519
Next
25212520
Console.WriteLine(output.ToString())
25222521

25232522
' This code produces the following output:
25242523
'
2525-
' All grades except the top three are:
2526-
' 82
2527-
' 70
2528-
' 59
2524+
' All grades except the first three are:
25292525
' 56
2526+
' 92
2527+
' 98
2528+
' 85
25302529
' </Snippet87>
25312530
End Sub
25322531
#End Region
@@ -2756,7 +2755,7 @@ Namespace SequenceExamples
27562755
{"grape", "passionfruit", "banana", "mango",
27572756
"orange", "raspberry", "apple", "blueberry"}
27582757

2759-
' Sort the strings first by their length and then
2758+
' Sort the strings first by their length and then
27602759
' alphabetically by passing the identity function.
27612760
Dim query As IEnumerable(Of String) =
27622761
fruits _
@@ -2802,7 +2801,7 @@ Namespace SequenceExamples
28022801
Dim fruits() As String =
28032802
{"apPLe", "baNanA", "apple", "APple", "orange", "BAnana", "ORANGE", "apPLE"}
28042803

2805-
' Sort the strings first by their length and then
2804+
' Sort the strings first by their length and then
28062805
' by using a custom "case insensitive" comparer.
28072806
Dim query As IEnumerable(Of String) =
28082807
fruits _
@@ -2895,7 +2894,7 @@ Namespace SequenceExamples
28952894
New Package With
28962895
{.Company = "Adventure Works", .Weight = 33.8, .TrackingNumber = 4665518773L}})
28972896

2898-
' Create a Dictionary that contains Package values,
2897+
' Create a Dictionary that contains Package values,
28992898
' using TrackingNumber as the key.
29002899
Dim dict As Dictionary(Of Long, Package) =
29012900
packages.ToDictionary(Function(p) p.TrackingNumber)
@@ -2929,7 +2928,7 @@ Namespace SequenceExamples
29292928
{"apple", "passionfruit", "banana", "mango",
29302929
"orange", "blueberry", "grape", "strawberry"}
29312930

2932-
' Project the length of each string and
2931+
' Project the length of each string and
29332932
' put the length values into a List object.
29342933
Dim lengths As List(Of Integer) =
29352934
fruits _
@@ -2979,9 +2978,9 @@ Namespace SequenceExamples
29792978
New Package With
29802979
{.Company = "Wide World Importers", .Weight = 33.8, .TrackingNumber = 4665518773L}})
29812980

2982-
' Create a Lookup to organize the packages.
2981+
' Create a Lookup to organize the packages.
29832982
' Use the first character of Company as the key value.
2984-
' Select Company appended to TrackingNumber
2983+
' Select Company appended to TrackingNumber
29852984
' as the element values of the Lookup.
29862985
Dim lookup As ILookup(Of Char, String) =
29872986
packages.ToLookup(Function(p) _
@@ -3049,15 +3048,15 @@ Namespace SequenceExamples
30493048

30503049
' This code produces the following output:
30513050
'
3052-
' 5
3053-
' 3
3054-
' 9
3055-
' 7
3056-
' 8
3057-
' 6
3058-
' 4
3059-
' 1
3060-
' 0
3051+
' 5
3052+
' 3
3053+
' 9
3054+
' 7
3055+
' 8
3056+
' 6
3057+
' 4
3058+
' 1
3059+
' 0
30613060
' </Snippet109>
30623061
End Sub
30633062
#End Region
@@ -3070,7 +3069,7 @@ Namespace SequenceExamples
30703069
{"apple", "passionfruit", "banana", "mango",
30713070
"orange", "blueberry", "grape", "strawberry"})
30723071

3073-
' Restrict the results to those strings whose
3072+
' Restrict the results to those strings whose
30743073
' length is less than six.
30753074
Dim query As IEnumerable(Of String) =
30763075
fruits.Where(Function(fruit) fruit.Length < 6)

xml/System.Linq/Enumerable.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10771,7 +10771,7 @@ If comparer is `null`, the default comparer <see cref="P:System.Collections.Gene
1077110771
In Visual Basic query expression syntax, a `Skip` clause translates to an invocation of <xref:System.Linq.Enumerable.Skip%2A>.
1077210772

1077310773
## Examples
10774-
The following code example demonstrates how to use <xref:System.Linq.Enumerable.Skip%2A> to skip a specified number of elements in a sorted array and return the remaining elements.
10774+
The following code example demonstrates how to use <xref:System.Linq.Enumerable.Skip%2A> to skip a specified number of elements in an array and return the remaining elements.
1077510775

1077610776
:::code language="csharp" source="~/snippets/csharp/System.Linq/Enumerable/AggregateTSource/enumerable.cs" interactive="try-dotnet-method" id="Snippet87":::
1077710777
:::code language="vb" source="~/snippets/visualbasic/VS_Snippets_CLR_System/system.Linq.Enumerable/VB/Enumerable.vb" id="Snippet87":::

0 commit comments

Comments
 (0)