DEV Community

Discussion on: Write a function that shows off something unique or interesting about the language you're using

Collapse
 
dance2die profile image
Sung M. Kim • Edited

That's amazing that Ruby and other languages can extend built-in classes as well.

Anyways, to show off C#, here you go.

C# can extend any classes using extension method syntax.

using System;

namespace extensionmethod
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!".Yell());
        }
    }

    public static class StringExtensions
    {
        public static string Yell(this string message)
        {
            return $"{message.ToUpper()}!!!";
        }
    }
}

Outputs

c:\misc\Sources\throwaway\extensionmethod>
$ dotnet run
HELLO WORLD!!!!

Note that you should mark the parameter with this

Yell(this string message)...