Introduction to functional programming in C # | Articles C # | C # | centre-equestre-lepuy.com ..


Sponsored Links

Introduction to functional programming in C #

Article written by Vincenzo Gaglio
Page 1 of 2

Functional programming is a style of writing source code that is not based on commands to be executed sequentially or in a certain order, but on the results of mathematical expressions (ie on functions). A function, as we well know, has as input a number of topics, which may also be other functions

Consider a simple loop that iterates over the numbers from 0 to 10 and displays them on the console.

(The article continues below ...)

The functional approach is as follows






 for (Int32 i = 0; i <10; i + +)



    



 Console.WriteLine (s);



Now let's see how to get the same result using functional programming. To do this you must create a helper method (ForEach <T>) in order to perform an action on each element of a collection (IEnumerable <T>) but also a function that returns a range of elements (Enumerable.Range ())






 public static class EstensioneFunzionale







 {



    



 public static void ForEach <T> (IEnumerable <T> element, Action <T> Run)



    



 {



        



 foreach (T item in the item)



            



 Run (item);



    



 }







 }









 public static class Enumerable







 {



    



 public IEnumerable <Int32> Range (Int32 from, Int32 to)



    



 {



        



 for (int i =, i <a, i + +)



            



 return i;



    



 }







 }



After defining the above functions we can achieve the same net effect of non-functional with the following code






 class Program







 {



    



 static void Main (string [] args)



    



 {



        



 EstensioneFunzionale.ForEach (Enumerable.Range (0, 10), View);



    



 }





    



 public void Show (Int32 i)



    



 {



        



 Console.WriteLine (s);



    



 }







 }



In the same category ...
E-Learning
Excel Course Excel Course
Create spreadsheets and calculation. At only 25 €.
Photoshop Course Photoshop Course
Web graphics and photo editing with Adobe Photoshop famous. Starting from 49 €.
Course Visual Basic 6 Course Visual Basic 6
Make desktop applications with VB6. Starting from 39 €.
Sponsored Links