Tech
Forums
Jobs
Books
Events
Interviews
Live
More
Learn
Training
Career
Members
Videos
News
Blogs
Contribute
Article
Blog
Video
Ebook
Interview Question
Collapse
Feed
Dashboard
Wallet
Learn
Achievements
Network
Rewards
SharpGPT
Premium
Contribute
Article
Blog
Video
Ebook
Interview Question
Register
Login
How to reverse a C# List
WhatsApp
Mahesh Chand
6y
230.4k
0
6
100
Article
C# List<T> class provides methods and properties to create a list of objects (classes). You can add items to a list during the initialization or using List.Add() and List.AddRange() methods.
List is a generic class. You must import the following namespace before using the List<T> class.
using System.Collections.Generic;
The Reverse method of List<T> reverses the order all items in in the List.
The following code example reverses a List.
// List of string
List<
string
> authors =
new
List<
string
>(5);
authors.Add(
"Mahesh Chand"
);
authors.Add(
"Chris Love"
);
authors.Add(
"Allen O'neill"
);
authors.Add(
"Naveen Sharma"
);
authors.Add(
"Mahesh Chand"
);
authors.Add(
"Monica Rathbun"
);
authors.Add(
"David McCarter"
);
Console.WriteLine(
"Original List items"
);
Console.WriteLine(
"==============="
);
// Print original order
foreach
(
string
a
in
authors)
Console.WriteLine(a);
// Reverse list items
authors.Reverse();
Console.WriteLine();
Console.WriteLine(
"Sorted List items"
);
Console.WriteLine(
"==============="
);
// Print reversed items
foreach
(
string
a
in
authors)
Console.WriteLine(a);
Listing 1.
The output of Listing 8 looks like Figure 1.
Figure 1.
Next >
C# List Tutorial
C# List
List.Reverse
Reverse List
reverse order List
Up Next
Ebook Download
View all
Printing in C# Made Easy
Read by 22.3k people
Download Now!
Learn
View all
Mindcracker
Founded in 2003, Mindcracker is the authority in custom software development and innovation. We put best practices into action. We deliver solutions based on consumer and industry analysis.
Membership not found