Click here to Skip to main content
Click here to Skip to main content

The Elegant Art of Programming

, 15 May 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
This tip uses the elegant LINQ to solve pesky daily problems.

Introduction

One of the most rewarding experiences of computer programming is to find more elegant ways of solving a problem. In this tip, I would like to first describe a realistic problem. Then show a longer way of solving it (dare I say, intuitive?) Finally show an elegant way of solving it in Microsoft LINQ.

The purpose of this tip is not to explain the details of LINQ, but show how it can be used in a simple to understand context. Reading the code should be very straightforward.

Problem

Imagine you have a list of double numbers, and we want to know if all the numbers in the list are the same, or not.

Solution 1

A simple console application in C# would solve this problem using a flag, a for loop, and a couple of if statements:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Solution1
{
    class Program
    {
       
        static void Main(string[] args)
        {
            List<double> myList = new List<double>();
            myList.Add(1.0);
            myList.Add(2.0);
            myList.Add(1.0);

            bool isSame = true;
            double track = -1.0;
            foreach (double num in myList)
            {
                if (track < 0.0)
                {
                    track = num;
                }
                else
                {
                    if (track != num)
                    {
                        isSame = false;
                        break;
                    }
                }
            }

            if (isSame)
            {
                Console.WriteLine("All numbers are the same.");
            }
            else
            {
                Console.WriteLine("All numbers are not the same.");
            }

            Console.ReadKey();
        }
    }
}  

Solution 2

Even if you have seen LINQ in action before, it's still fun to see it again:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Solution2
{
    class Program
    {
        static void Main(string[] args)
        {
            List<double> myList = new List<double>();
            myList.Add(1.0);
            myList.Add(1.0);
            myList.Add(1.0);

            var query1 = (from num in myList select num).Distinct();

            if (query1.Count() > 1)
            {
                Console.WriteLine("All numbers are not the same.");
            }
            else
            {
                Console.WriteLine("All numbers are the same.");
            }

            Console.ReadKey();
        }
    }
} 

Points of Interest

While both programs do the same thing, the solution in LINQ is an order of magnitude times smaller. It focuses on the algorithm and not the loop, as Bjarne Stroustrup encourages all programmers to do so. Attached are the projects for solutions 1 and 2.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Erol Esen
Software Developer (Senior)
United States United States
No Biography provided

Comments and Discussions

 
GeneralMy vote of 3 [modified] PinmemberGuillaume Leparmentier18-May-12 9:28 
GeneralRe: My vote of 3 PinmemberErol Esen18-May-12 10:17 
GeneralRe: My vote of 3 PinmemberGuillaume Leparmentier18-May-12 10:58 
GeneralRe: My vote of 3 PinmemberErol Esen20-May-12 13:36 
Questionyet another solution... PinmemberAndreas Gieriet15-May-12 5:58 
GeneralRe: yet another solution... PinmemberPIEBALDconsult15-May-12 8:08 
GeneralRe: yet another solution... [modified] PinmemberAndreas Gieriet15-May-12 10:25 
GeneralRe: yet another solution... PinmemberErol Esen15-May-12 11:18 
GeneralRe: yet another solution... PinmemberPIEBALDconsult15-May-12 12:46 
GeneralRe: yet another solution... PinmemberAndreas Gieriet15-May-12 13:03 
GeneralRe: yet another solution... PinmemberPIEBALDconsult15-May-12 14:28 
GeneralRe: yet another solution... PinmemberAndreas Gieriet15-May-12 23:40 
Questioneven simpler PinmemberAndreas Gieriet15-May-12 5:32 
SuggestionDon't do that with real values (double, float, decimals)! PinmemberAndreas Gieriet15-May-12 5:03 
GeneralRe: Don't do that with real values (double, float, decimals)! PinmemberErol Esen15-May-12 5:30 
GeneralRe: Don't do that with real values (double, float, decimals)! PinmemberAndreas Gieriet15-May-12 5:43 
SuggestionAlternatives... PinmemberAndrew Rissing15-May-12 4:42 
GeneralRe: Alternatives... PinmemberErol Esen15-May-12 5:25 
Suggestion..or .. Pinmemberednrg15-May-12 4:36 
GeneralRe: ..or .. Pinmemberdelscorcho1@gmail.com17-May-12 6:22 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web03 | 2.8.141022.2 | Last Updated 15 May 2012
Article Copyright 2012 by Erol Esen
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid