Voting

Category

real language

Bookmarking

Del.icio.us Digg Diigo DZone Earthlink Google Kick.ie
Windows Live LookLater Ma.gnolia Reddit Rojo StumbleUpon Technorati

Language C#

(Shows new features of C# 2.0.)

Date:11/24/05
Author:Bradley Tetzlaff
URL:http://3d7software.org
Comments:0
Info:http://msdn.microsoft.com/vcsharp/programming/language/
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>
    [CLSCompliant(true)]
    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,
{1}";
            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),
                        LYRICS_NONE);
                }
                else if (bi == 0)   // No beers.
                    return LYRICS_ZERO;
                else
                    throw new IndexOutOfRangeException("Invalid beer amount.");
            };
            // Sing all 99.
            for (int bi = 99; bi >= 0; bi--)
            {
                Console.WriteLine(sing(bi));
                Console.ReadLine();
            }
        }
    }
}

Download Source | Write Comment

Alternative Versions

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

Comments

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!

Name:

eMail:

URL:

Security Code:
  
Comment: