0

I love C# but, for example, a simple a conditional like the following is quite verbose for what it's trying to achieve:

if ( (ctr == "BT") || (ctr = "B") ) {
  ctrName = "Brian";
} else if ( (ctr == "G") || (ctr = "GD") ) {
  ctrName = "George";
}

I was thinking that in an ideal language it could be coded something like:

if ctr: 
  in {"BT", "B"}: ctr = "Brian"
  in {"G", "GD" }: ctr = "George"

Is there any language more like that?

MrGreggles
  • 6,113
  • 9
  • 42
  • 48
  • Are those two single =s typos? – BoltClock Aug 12 '17 at 05:41
  • why not linq to dictionary – Ramankingdom Aug 12 '17 at 05:45
  • Depending on what you are trying to match, a `RegEx` match may be appropriate (not for this case but for something where you are trying to match more complex variations). – Ian Mercer Aug 12 '17 at 06:41
  • 1
    Or if you can guarantee that `ctr` doesn't contain a specific character like `|` you can use `String.Contains`, e.g. `"BT|B".Contains(ctr)`. Or you can make it case insensitive too (see https://stackoverflow.com/a/444818/224370) – Ian Mercer Aug 12 '17 at 06:45
  • @BoltClock Oops, yeah [blush] – MrGreggles Aug 12 '17 at 06:54
  • @mjwills I left this with a bit of a conundrum in that BoltClock's solution got a bunch of up votes and is a fine reply but I like your extension method as being closer to what I was looking for. Not wanting to upset the apple cart I ended up not knowing which to mark as the "best" answer. Is there a "best" answer here? Both seem good to me but I the extension approach, so in fact going with that. – MrGreggles Aug 17 '17 at 05:35
  • Whatever you decide is fine @MrGreggles. – mjwills Aug 17 '17 at 06:22

2 Answers2

5

You can easily do this in many languages, including C#, with a switch statement:

switch (ctr)
{
    case "BT":
    case "B":
        ctrName = "Brian";
        break;

    case "G":
    case "GD":
        ctrName = "George";
        break;
}

The same switch statement with reduced line breaks to get somewhat closer to your example:

switch (ctr)
{
    case "BT": case "B": ctrName = "Brian"; break;
    case "G": case "GD": ctrName = "George"; break;
}

If that's too verbose for you, then there are no other alternatives in C#. If you're specifically looking for an alternative language, that's off-topic for the site.

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
1

There is a language just like that! C# - via extension methods.

Here is an example, using an In extension method.

using System;
using System.Linq;

namespace Bob
{
    public static class Extensions
    {
        public static bool In<T>(this T toCheck, params T[] isItInOneOfThese)
        {
            return isItInOneOfThese.Contains(toCheck);
        }
    }

    public class Program
    {
        static void Main(string[] args)
        {
            var ctrName = "";
            var ctr = "BT";

            if (ctr.In("BT", "B"))
            {
                ctrName = "Brian";
            }
            else if (ctrName.In("G", "GD"))
            {
                ctrName = "Brian";
            }
            Console.WriteLine(ctrName);
            Console.ReadLine();
        }
    }
}
mjwills
  • 23,389
  • 6
  • 40
  • 63