none
Compare input string with Lambda expression RRS feed

  • Question

  • Hi Guys,

    I have simple query in C# with Lambda expression. If I dont want to use IF/ELSE and Switch then I can use Lambda. I did that but not getting proper output

    Below is my simple sample code

    static void Main(string[] args)
            {
    Console.WriteLine("Please provide input (ex-a/b/b etc):");
                string s = Console.ReadLine();
                string sOut = string.Empty;
                var sCheck = new Dictionary<string, Func<string>>()
                {
                    { "a", () => sOut = "Some logic here"},
                    { "b", () => sOut ="Something else here"},
                    { "c", () => sOut ="Ofcourse more than just print statement"}
                };
    
                Console.WriteLine("Output for {0} should be : {1}",s,sOut);
                Console.ReadLine();
    }

    So if i enter a it should print statement for a. but I am getting blank.


    Abhijeet Khopade

    Friday, March 27, 2020 8:48 PM

Answers

  • This worked for me:

    static void Main(string[] args)
          {
             Console.WriteLine("Please provide input (ex-a/b/b etc):");
             string s = Console.ReadLine();
             string sOut = string.Empty;
             var sCheck = new Dictionary<string, Func<string>>()
                {
                    { "a", () => sOut = "Some logic here"},
                    { "b", () => sOut ="Something else here"},
                    { "c", () => sOut ="Ofcourse more than just print statement"}
                };
    
             Func<string> result;
             sCheck.TryGetValue(s, out result);
    
             var output = result.Invoke();
             Console.WriteLine("Output for {0} should be : {1}", s, output);
             Console.ReadLine();
          }


    For every expert, there is an equal and opposite expert. - Becker's Law


    My blog


    My TechNet articles


    Friday, March 27, 2020 8:56 PM
    Moderator

All replies

  • This worked for me:

    static void Main(string[] args)
          {
             Console.WriteLine("Please provide input (ex-a/b/b etc):");
             string s = Console.ReadLine();
             string sOut = string.Empty;
             var sCheck = new Dictionary<string, Func<string>>()
                {
                    { "a", () => sOut = "Some logic here"},
                    { "b", () => sOut ="Something else here"},
                    { "c", () => sOut ="Ofcourse more than just print statement"}
                };
    
             Func<string> result;
             sCheck.TryGetValue(s, out result);
    
             var output = result.Invoke();
             Console.WriteLine("Output for {0} should be : {1}", s, output);
             Console.ReadLine();
          }


    For every expert, there is an equal and opposite expert. - Becker's Law


    My blog


    My TechNet articles


    Friday, March 27, 2020 8:56 PM
    Moderator
  • Thanks a lot Naomi,

    Worked perfectly.


    Abhijeet Khopade

    Friday, March 27, 2020 11:37 PM