## Something bad happened

We're not sure what, but we have a few guesses.

Problem: Object reference not set to an instance of an object.
Ticket: 7864823
Server: Web01
The Elegant Art of Programming - CodeProject
11,715,043 members (69,010 online)

# The Elegant Art of Programming

, 15 May 2012 CPOL 9.5K 2
 Rate this:
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>();

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.");
}

}
}
}  ```

## 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>();

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.");
}

}
}
} ```

## 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.

## About the Author

 Software Developer (Senior) United States
No Biography provided

## Comments and Discussions

 First Prev Next
 My vote of 3 Guillaume Leparmentier18-May-12 9:28 Guillaume Leparmentier 18-May-12 9:28
 Re: My vote of 3 Erol Esen18-May-12 10:17 Erol Esen 18-May-12 10:17
 Re: My vote of 3 Guillaume Leparmentier18-May-12 10:58 Guillaume Leparmentier 18-May-12 10:58
 Re: My vote of 3 Erol Esen20-May-12 13:36 Erol Esen 20-May-12 13:36
 yet another solution... Andreas Gieriet15-May-12 5:58 Andreas Gieriet 15-May-12 5:58
 Re: yet another solution... PIEBALDconsult15-May-12 8:08 PIEBALDconsult 15-May-12 8:08
 Re: yet another solution... Andreas Gieriet15-May-12 10:25 Andreas Gieriet 15-May-12 10:25
 Re: yet another solution... Erol Esen15-May-12 11:18 Erol Esen 15-May-12 11:18
 Re: yet another solution... PIEBALDconsult15-May-12 12:46 PIEBALDconsult 15-May-12 12:46
 Re: yet another solution... Andreas Gieriet15-May-12 13:03 Andreas Gieriet 15-May-12 13:03
 Re: yet another solution... PIEBALDconsult15-May-12 14:28 PIEBALDconsult 15-May-12 14:28
 Re: yet another solution... Andreas Gieriet15-May-12 23:40 Andreas Gieriet 15-May-12 23:40
 even simpler Andreas Gieriet15-May-12 5:32 Andreas Gieriet 15-May-12 5:32
 Don't do that with real values (double, float, decimals)! Andreas Gieriet15-May-12 5:03 Andreas Gieriet 15-May-12 5:03
 Re: Don't do that with real values (double, float, decimals)! Erol Esen15-May-12 5:30 Erol Esen 15-May-12 5:30
 Re: Don't do that with real values (double, float, decimals)! Andreas Gieriet15-May-12 5:43 Andreas Gieriet 15-May-12 5:43
 Alternatives... Andrew Rissing15-May-12 4:42 Andrew Rissing 15-May-12 4:42
 Re: Alternatives... Erol Esen15-May-12 5:25 Erol Esen 15-May-12 5:25
 ..or .. ednrg15-May-12 4:36 ednrg 15-May-12 4:36
 Re: ..or .. delscorcho1@gmail.com17-May-12 6:22 delscorcho1@gmail.com 17-May-12 6:22
 Last Visit: 31-Dec-99 18:00     Last Update: 2-Sep-15 9:18 Refresh 1

General    News    Suggestion    Question    Bug    Answer    Joke    Rant    Admin

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