Server Intellect Cloud Hosting

Fun with C# Lists (Part 1 of 3)

Category: .NET Framework

 

Fun with C# Lists… both of them. Part One of Three


Introduction

In this article we will be working with List< Type>, in 2.0 C# implemented List<Type> to allow you to utilize Lists of any kind and to easily manipulate those Lists with some built in methods.  We will explore a couple of those methods in this article and demonstrate how easy it is to work with List< Type>.  Those methods are:

Server Intellect assists companies of all sizes with their hosting needs by offering fully configured server solutions coupled with proactive server management services. Server Intellect specializes in providing complete internet-ready server solutions backed by their expert 24/365 proactive support team.

Need help with Windows Dedicated Hosting? Try Server Intellect. I'm a happy customer!

  • Sort
  • FindAll

We have all had difficulty working with lists of any kind, we would have to use arrays and list boxes and write a lot of code to handle a problem that should be so simple.

Implementation

In this article we will be working with a class called “Automobile”, this class has two attributes called

  • Seats
  • Model

The Seats attribute denotes the seating capacity of the vehicle and the Model attribute denotes the Model of the Automobile.  These two attributes make up the two values we will be working with when sorting and searching through our List.

Automobile Class
This is the Automobile class that describes all of the attributes that are shared amongst all models of Automobile that derive from this class. 

If you're looking for a really good web host, try Server Intellect - we found the setup procedure and control panel, very easy to adapt to and their IT team is awesome!

public class Automobile
{
public int seats;
public string model;
public Automobile(int seats, string model)
{
this.seats = seats;
this.model = model;
}
}

I just signed up at Server Intellect and couldn't be more pleased with my Windows Server! Check it out and see for yourself.

Declare the List

This is where we declare the List.  We assign it a Type of Automobile.  What we are saying is that we need a collection of Automobile Classes to work with.

List<Automobile> auto = new List<Automobile>();

 

Populate the List with values

We are populating the List with all of the Models and the seating capacity of those Models.  In this example we are using 6 different Models, each one with a unique name and with a unique seating capacity.

auto.Add(new Automobile(2, "Corvette"));
auto.Add(new Automobile(4, "Mustang"));
auto.Add(new Automobile(5, "Accord"));
auto.Add(new Automobile(3, "F-150"));
auto.Add(new Automobile(7, "Entourage"));
auto.Add(new Automobile(8, "Pilot"));

We used over 10 web hosting companies before we found Server Intellect. Their dedicated servers and add-ons were setup swiftly, in less than 24 hours. We were able to confirm our order over the phone. They respond to our inquiries within an hour. Server Intellect's customer support and assistance are the best we've ever experienced.

Sort the List by Model

What we are trying to accomplish here is to be able to sort the List by the Model.  This is an alphabetical sort using the first character of each word.  We are using the built in “Sort” method of the List holding all of our Models.

auto.Sort(delegate(Automobile auto1, Automobile auto2) { return auto1.model.CompareTo(auto2.model); });

Please see Fig. 1 for the results of the Model search.  You will notice that we only had to use one line of code in order to accomplish our sort.  This is one of the benefits of List<Type> and why it can be so efficient for all of your collection needs.
In the next example we will sort by the seating capacity of the Automobile.

Server Intellect offers Windows Hosting Dedicated Servers at affordable prices. I'm very pleased!

Display of the results of the Model sort [Fig. 1]:

Image 1
[Click to see full-size]

If you're ever in the market for some great Windows web hosting, try Server Intellect. We have been very pleased with their services and most importantly, technical support.


What have we learned?

Try Server Intellect for Windows Server Hosting. Quality and Quantity!

  • Creating the Automobile class
  • How to Populate the List
  • How to sort the list by Model

Next Up

The next part in this series of articles will get into how we can utilize other methods in the List class.

Continue reading Part Two

Attachments



Download Project Source - Enter your Email to be emailed a link to download the Full Source Project used in this Tutorial!



100% SPAM FREE! We will never sell or rent your email address!