Monthly Archives: April 2016

String Interpolation in C# 6

We regularly use string.Format or string.Concat or “+” operator to do all kinds of string manipulations.

public class Person
{
    public string FirstName { get; set; }
    public string MiddleInitial { get; set; }
    public string LastName { get; set; }
}

public void PrintPersonName(Person person)
{
    Console.WriteLine(string.Format("Full Name: {0} {1} {2}", person.FirstName, person.LastName, person.MiddleInitial));
}

String.Format and its cousins are very versatile, but their use is somewhat clunky and error prone. Particularly unfortunate is the use of numbered placeholders like {0} in the format string, which must line up with separately supplied arguments.

This creates new problems:

  • You have to maintain the index yourself. If the number of arguments and index are not the same, it will generate error.
  • If you need to add a new item to the string (not to the end), you have to update the whole indexing number.

For those reasons, we should use C# 6 new feature String interpolation.

public void PrintPersonNameNewWay(Person person)
{
            Console.WriteLine($"Full Name: {person.FirstName} {person.LastName} {person.MiddleInitial}");
}

The compiler now maintains the placeholders for you so you don’t have to worry about indexing the right argument because you simply place it right there in the string.  In String Interpolation, we simply prefix the string with a $ (much like we use the @ for verbatim strings). Then, we simply surround the expressions we want to interpolate with curly braces (i.e. { and }):

Advertisements

Compute number of ways to make amount of money with coins of the available denominations.

Below code snippet is easiest way to compute number of ways to make amount of money with coins of the available denominations.

Example: for amount=20 and denominations 20, 10, 5, 1, program should output 10 ways.

    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(MaxPossibleCombinations(4, new int[] { 1,2,3}));
            Console.WriteLine(MaxPossibleCombinations(20, new int[] { 20, 10, 5, 1 }));
            Console.ReadLine();
        }

        public static int MaxPossibleCombinations(int amount, int[] denominations)
        {
            int[] wayofDoingNAmount = new int[amount + 1];
            wayofDoingNAmount[0] = 1;
            foreach (int coin in denominations)
            {
                for (int higherAmount = coin; higherAmount < amount + 1; higherAmount++)
                {
                    int remainderAmount = higherAmount - coin;
                    wayofDoingNAmount[higherAmount] += wayofDoingNAmount[remainderAmount];
                }
            }
            return wayofDoingNAmount[amount];
        }
    }

This can also be achieved using the memoize technique. Memoization ensures that a function doesn’t run for the same inputs more than once by keeping a record of the results for given inputs (usually in a hash map).

ngSimpleMultiSelectDropdown

I had requirement at my job to implement simple multi select drop down and after quite a bit of research I could not find any thing that was simple, light weight and nimble. Available ones are too fancy and I did not wanted to carry unnecessary baggage of those, so decided to build my own.

So I have built light weight angular directive for simple multi select drop down.

You can find the demo over here

Features

  • Simple built using jQuery abstracted with angular directive
  • Select all, deselect all items, down arrow key expands, esc collapses.
  • Light weight compared to what is available over web.
  • Customizable

Further Details can be found at github over here here

Published in ngmodule as well over Further Details can be found at github over here here