C# Program to Sort a List of Names in Alphabetical Order

This is a C# Program to sort a list of names in alphabetical order.

Problem Description

This C# Program Sorts a List of Names in Alphabetical Order.

Problem Solution

Here the List of names are sorted with the help of sort function.

Program/Source Code

Here is source code of the C# Program to Sort a List of Names in Alphabetical Order. The C# program is successfully compiled and executed with Microsoft Visual Studio. The program output is also shown below.

/*
 * C# Program to Sort a List of Names in Alphabetical Order
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
    class Program
    {
        static void Main()
        {
            List<string> names = new List<string>();
            names.Add("Ram");
            names.Add("Rose");
            names.Add("Abs");
            names.Add("Edward");
            names.Add("Sita");
            names.Sort();
            foreach (string s in names) 
            Console.WriteLine(s);
            Console.ReadLine();
        }
    }
}
Program Explanation

In this C# program, using foreach loop print the names in alphabetical order. The foreach statement is used to iterate through the collection to get the information that we want, but cannot be used to add or remove items from the source collection to avoid unpredictable side effects. Print the sorted list of names in alphabetical order.

advertisement
advertisement
Runtime Test Cases
 
Abs
Edward
Ram
Rose
Sita

Sanfoundry Global Education & Learning Series – 1000 C# Programs.

If you wish to look at all C# Programming examples, go to 1000 C# Programs.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.