C# 7.0 – #7. An improved expression body feature

In C# 6.0 a read-only property like this:

public string SomeProperty
{
    get { return "sometext"; }
}

can be rewritten in a more compact way:

public string SomeProperty => "sometext";

This feature is called “expression body”, but it has some limitations, e.g. the property is turned into field, it only works with read-only properties and not with constructors, deconstructor, getter, setter and so on.

C# 7.0 add all these constructs and then expands the usage of this feature.

Here’s an example, which was taken “as is” from the .Net blog without any test in development environment because by the time of the last released build of Visual Studio 2017 (15.0.26020.0) this new feature doesn’t work yet:

class Person
{
    private static ConcurrentDictionary<int, string> names = new ConcurrentDictionary<int, string>();
    private int id = GetId();

    public Person(string name) => names.TryAdd(id, name); // constructors

    ~Person() => names.TryRemove(id, out *);              // destructors
    
    public string Name
    {
        get => names[id];                                 // getters
        set => names[id] = value;                         // setters
    }
}

256 thoughts on “C# 7.0 – #7. An improved expression body feature

  1. Pingback: buy viagra usa
  2. Pingback: buy tadalafil
  3. Pingback: buy cialis germany
  4. Pingback: mazhor4sezon
  5. Pingback: canadian drugs
  6. Pingback: canada drug
  7. Pingback: filmfilmfilmes
  8. Pingback: gRh9UPV
  9. Pingback: canada pharmacy
  10. Pingback: gewrt.usluga.me
  11. Pingback: 9-05-2022
  12. Pingback: kinoteatrzarya.ru
  13. Pingback: canadadrugs
  14. Pingback: TopGun2022
  15. Pingback: Xvideos
  16. Pingback: XVIDEOSCOM Videos
  17. Pingback: canadian cialis
  18. Pingback: northwestpharmacy
  19. Pingback: ivanesva
  20. Pingback: canadadrugs
  21. Pingback: Netflix
  22. Pingback: FILM
  23. Pingback: designchita.ru
  24. Pingback: YA-krasneyu
  25. Pingback: design-human.ru
  26. Pingback: designmsu.ru
  27. Pingback: vkl-design.ru
  28. Pingback: irida-design.ru
  29. Pingback: projectio
  30. Pingback: psy online
  31. Pingback: do-posle-psihologa
  32. Pingback: uels ukrain
  33. Pingback: online pharmacy
  34. Pingback: DPTPtNqS
  35. Pingback: qQ8KZZE6
  36. Pingback: D6tuzANh
  37. Pingback: SHKALA TONOV
  38. Pingback: chelovek-iz-90-h
  39. Pingback: 3Hk12Bl
  40. Pingback: 3NOZC44
  41. Pingback: 01211
  42. Pingback: tor-lyubov-i-grom
  43. Pingback: film-tor-2022
  44. Pingback: hd-tor-2022
  45. Pingback: hdorg2.ru
  46. Pingback: canadian pharmacys
  47. Pingback: Psikholog
  48. Pingback: netstate.ru
  49. Pingback: Link
  50. Pingback: buy viagra 25mg
  51. Pingback: psy
  52. Pingback: bit.ly
  53. Pingback: cleantalkorg2.ru
  54. Pingback: bucha killings
  55. Pingback: War in Ukraine
  56. Pingback: Ukraine
  57. Pingback: sildenafil
  58. Pingback: site
  59. Pingback: viagra
  60. Pingback: stats
  61. Pingback: revatio
  62. Pingback: Ukraine-war
  63. Pingback: movies
  64. Pingback: gidonline
  65. Pingback: web
  66. Pingback: film.8filmov.ru
  67. Pingback: video
  68. Pingback: filmgoda.ru
  69. Pingback: rodnoe-kino-ru
  70. Pingback: stat.netstate.ru
  71. Pingback: sY5am
  72. Pingback: Dom drakona
  73. Pingback: JGXldbkj
  74. Pingback: aOuSjapt
  75. Pingback: ìûøëåíèå
  76. Pingback: psikholog moskva
  77. Pingback: Dim Drakona 2022
  78. Pingback: TwnE4zl6
  79. Pingback: psy 3CtwvjS
  80. Pingback: film onlinee
  81. Pingback: canada drug
  82. Pingback: viagra canada
  83. Pingback: buy stromectol
  84. Pingback: stromectol doses
  85. Pingback: 3qAIwwN
  86. Pingback: video-2
  87. Pingback: sezons.store
  88. Pingback: psy-news.ru
  89. Pingback: buy viagra 25mg
  90. Pingback: 000-1
  91. Pingback: 3SoTS32
  92. Pingback: 3DGofO7
  93. Pingback: canada drugs
  94. Pingback: stromectol cvs

Comments are closed.