C# 7.0 – #4. Local functions

In C# 6.0 there is no way to declare a function which is local to another function, that is a function visible only inside the body in which is declared. The best way to accomplish this is to declare a delegate variable of type Func<T1,TResult> (or one of the several overloads available), ad then use anonymous methods or lambda expression to write the code of the function, as shown here.

// c# 6.0
Func<int, int, int> sum = (k, y) => {
      return k + y;
};

int c = sum(a, b);

This is not an optimal way to use local function, because of some limitations.

C# 7.0 comes with a true local function, and overcomes all the previous limitations.

Here is an example:

int DoSum(int a, int b)
{
   return LocalSum(a, b);

   int LocalSum(int x, int y)
   {
      return x + y;
   }
}

This is a more natural way to declare and use a local function.

217 thoughts on “C# 7.0 – #4. Local functions

  1. Pingback: online drug store
  2. Pingback: buy cialis cheap
  3. Pingback: buy cialis cheap
  4. Pingback: mazhor4sezon
  5. Pingback: filmfilmfilmes
  6. Pingback: online pharmacies
  7. Pingback: cialis from canada
  8. Pingback: canadian drugstore
  9. Pingback: online drug store
  10. Pingback: pharmacy
  11. Pingback: canada rx
  12. Pingback: canadian drugstore
  13. Pingback: canada viagra
  14. Pingback: online pharmacy
  15. Pingback: pharmacy canada
  16. Pingback: drugs for sale
  17. Pingback: Gz92uNNH
  18. Pingback: drugs for sale
  19. Pingback: online pharmacy
  20. Pingback: online pharmacy
  21. Pingback: JXNhGmmt
  22. Pingback: Psikholog
  23. Pingback: netstate.ru
  24. Pingback: Link
  25. Pingback: psy
  26. Pingback: bit.ly
  27. Pingback: cleantalkorg2.ru
  28. Pingback: bucha killings
  29. Pingback: War in Ukraine
  30. Pingback: Ukraine
  31. Pingback: site
  32. Pingback: stats
  33. Pingback: Ukraine-war
  34. Pingback: movies
  35. Pingback: gidonline
  36. Pingback: web
  37. Pingback: film.8filmov.ru
  38. Pingback: video
  39. Pingback: film
  40. Pingback: filmgoda.ru
  41. Pingback: rodnoe-kino-ru
  42. Pingback: confeitofilm
  43. Pingback: stat.netstate.ru
  44. Pingback: sY5am
  45. Pingback: Dom drakona
  46. Pingback: JGXldbkj
  47. Pingback: aOuSjapt
  48. Pingback: ìûøëåíèå
  49. Pingback: psikholog moskva
  50. Pingback: Usik Dzhoshua 2
  51. Pingback: Dim Drakona 2022
  52. Pingback: TwnE4zl6
  53. Pingback: psy 3CtwvjS
  54. Pingback: lalochesia
  55. Pingback: canadian drugs
  56. Pingback: film onlinee
  57. Pingback: drugstore online
  58. Pingback: canada medication
  59. Pingback: northwestpharmacy
  60. Pingback: canada drug
  61. Pingback: online pharmacies
  62. Pingback: 3qAIwwN
  63. Pingback: video-2
  64. Pingback: sezons.store
  65. Pingback: stromectol coupon
  66. Pingback: 000-1
  67. Pingback: 3SoTS32
  68. Pingback: 3DGofO7
  69. Pingback: Northwest Pharmacy

Comments are closed.