Walkthrough Func type in C#

from my last post I talked about the Func type and Action type. so today also i'm going to go through that topic and show you a sample code of func type usage. Code is pretty simple and no need to explain in detail. go through it then you will understand that easily and also you'll get an idea about the Func type. of easy of demonstration i put all the method to program.cs class in console application project.

Enums and Dictionary variables
     /// <summary>  
     /// The dictionary to hold method names  
     /// </summary>  
     private static Dictionary<CalculationType, Func<int, int, int>> methodDictionary;  
   
     /// <summary>  
     /// CalculationType  
     /// </summary>  
     internal enum CalculationType  
     {  
       Add,  
       Multiply,  
       Subtract,  
       Divide  
   
     }  

Common Calculation methods
     /// <summary>  
     /// Adds Two values and return the answer.  
     /// </summary>  
     /// <param name="valueOne">value one.</param>  
     /// <param name="valueTwo">value two.</param>  
     /// <returns></returns>  
     public static int Add(int valueOne, int valueTwo)  
     {  
       return valueOne + valueTwo;  
     }  
   
     /// <summary>  
     /// Multiplies Two values and return the answer.  
     /// </summary>  
     /// <param name="valueOne">value one.</param>  
     /// <param name="valueTwo">value two.</param>  
     /// <returns></returns>  
     public static int Multiply(int valueOne, int valueTwo)  
     {  
       return valueOne * valueTwo;  
     }  
   
     /// <summary>  
     /// Subtracts Two values and return the answer.  
     /// </summary>  
     /// <param name="valueOne">value one.</param>  
     /// <param name="valueTwo">value two.</param>  
     /// <returns></returns>  
     public static int Subtract(int valueOne, int valueTwo)  
     {  
       return valueOne - valueTwo;  
     }  
   
     /// <summary>  
     /// Divides Two values and return the answer.  
     /// </summary>  
     /// <param name="valueOne">value one.</param>  
     /// <param name="valueTwo">value two.</param>  
     /// <returns></returns>  
     public static int Divide(int valueOne, int valueTwo)  
     {  
       return valueOne / valueTwo;  
     }  

Func Type Implementation methods and Main method
     /// <summary>  
     /// Mains methods.  
     /// </summary>  
     static void Main()  
     {  
       Console.WriteLine("Add Method : "+Calculation(CalculationType.Add, 4,3));  
       Console.WriteLine("Multiply Method : " + Calculation(CalculationType.Multiply, 10, 5));  
       Console.WriteLine("Divide Method : " + Calculation(CalculationType.Divide, 10, 5));  
       Console.WriteLine("Subtract Method : " + Calculation(CalculationType.Subtract, 10, 8));  
       Console.ReadLine();  
     }  
   
     /// <summary>  
     /// Calculations Method  
     /// </summary>  
     /// <param name="type">calculation type.</param>  
     /// <param name="valueOne">value one.</param>  
     /// <param name="valueTwo">value two.</param>  
     /// <returns></returns>  
     public static int Calculation(CalculationType type, int valueOne, int valueTwo)  
     {  
       var methodName = GetMethodName(type);  
       return methodName(valueOne, valueTwo);  
   
     }  
   
     /// <summary>  
     /// Gets the name of the method.  
     /// </summary>  
     /// <param name="typeKey">The type key.</param>  
     /// <returns></returns>  
     private static Func<int, int, int> GetMethodName(CalculationType typeKey)  
     {  
       Func<int, int, int> methodName = null;  
   
       // Check dictionary instance is null or not  
       if (methodDictionary == null)  
       {  
         // if null then create a new instance and add method values and key to it.  
         methodDictionary = new Dictionary<CalculationType, Func<int, int, int>>  
           {  
             { CalculationType.Add, Add },  
             { CalculationType.Multiply, Multiply },  
             { CalculationType.Subtract, Subtract },  
             { CalculationType.Divide, Divide }  
           };  
       }  
          
       // Check dictionary contain the key value we passed as parameter  
       // if available then assign value to the method   
       if (methodDictionary.ContainsKey(typeKey))  
       {  
         methodName = methodDictionary[typeKey];  
       }  
       return methodName;  
     }  

now i put all the code segment that should come to the program.cs class now it's time to run and see the result. so if you did it correctly then you will get an output like below. That's happy coding guys. if you have any question related to this article please put that one as a comment below. 

Comments

Popular posts from this blog

Deep dive into OpenXML Part 1

Deep dive into OpenXML Part 2

Convert XML data to dynamic (ExpandoObject) Object