real language

Bookmarking Digg Diigo DZone Earthlink Google
Windows Live LookLater Ma.gnolia Reddit Rojo StumbleUpon Technorati

Language C#

(Shows new features of C# 2.0.)

Author:Bradley Tetzlaff
Score: (2.50 in 8 votes)
// C# 2.0 Ninety Nine Bottles of Beer Example
// Bradley Tetzlaff
// 2005-11-23

using System;

namespace NinetyNineBottlesOfBeer
    /// <summary>
    /// An example of the 99 Bottles of Beer song in C# 2.0,
    /// with intentional use of new features of version 2.0.
    /// </summary>
    public static class NinetyNineBottlesOfBeerSong
        /// <summary>Singing method type.</summary>
        /// <param name="bi">The bottle index.</param>
        /// <returns>The song verse.</returns>
        private delegate string SingVerseMethod(int bi);

        /// <summary>The pluralization method type.</summary>
        /// <param name="bi">The bottle index.</param>
        /// <returns>
        /// An &quot;bottles&quot; if plural, otherwise &quot;bottle&quot;.
        /// </returns>
        private delegate string Pluralizer(int bi);

        /// <summary>
        /// Prints the 99 Bottles of Beer song to the console.
        /// </summary>
        public static void Main()
            // Lyric Parts
            const string LYRICS_SOME = @"
{0} of beer on the wall,
{0} of beer.
Take one down, pass it around,
            const string LYRICS_NEXT = " of beer on the wall.";
            const string LYRICS_NONE = "No more bottles of beer on the wall.";

            const string LYRICS_ZERO = @"
No more bottles of beer on the wall,
no more bottles of beer.
Go to the store and buy some more,
99 bottles of beer on the wall.";

            // Anonymous Methods
            // Returns the correct pluralization of "bottle".
            Pluralizer bottles = delegate(int bi)
                { return (bi == 1) ? "1 bottle" : bi + " bottles"; };

            // Sings one verse of the 99 Bottles of Beer song.
            SingVerseMethod sing = delegate(int bi)
                if (bi > 0) // More than one beer.
                    return string.Format(LYRICS_SOME, bottles(bi),
                        bottles(bi - 1) + LYRICS_NEXT);
                else if (bi == 1)   // One beer.
                    return string.Format(LYRICS_SOME, bottles(bi),
                else if (bi == 0)   // No beers.
                    return LYRICS_ZERO;
                    throw new IndexOutOfRangeException("Invalid beer amount.");
            // Sing all 99.
            for (int bi = 99; bi >= 0; bi--)

Download Source | Write Comment

Alternative Versions

Concise, using C# 3.5 featuresJeff Dietrich10/26/0714
2Paul M. Parks04/20/059
C# Gratuitously FunctionalPaul Stancer04/14/091
corporate styleveteran corporate coder08/16/090
5Mark Hurley05/31/050
Uses LinqSudipta11/19/100
v3.0 Easy Functional RecursiveYelinna Pulliti Carrasco08/04/090


Download Source | Write Comment

Add Comment

Please provide a value for the fields Name, Comment and Security Code.
This is a gravatar-friendly website.
E-mail addresses will never be shown.
Enter your e-mail address to use your gravatar.

Please don't post large portions of code here! Use the form to submit new examples or updates instead!




Security Code: