﻿
{"id":27113,"date":"2024-08-11T15:10:24","date_gmt":"2024-08-11T15:10:24","guid":{"rendered":"https:\/\/www.gencayyildiz.com\/blog\/?p=27113"},"modified":"2024-08-11T15:10:24","modified_gmt":"2024-08-11T15:10:24","slug":"net-8-collection-expressions","status":"publish","type":"post","link":"https:\/\/www.gencayyildiz.com\/blog\/net-8-collection-expressions\/","title":{"rendered":".NET 8 &#8211; Collection Expressions"},"content":{"rendered":"<div id=\"fb-root\"><\/div>\n<p>Merhaba,<\/p>\n<p>Bu i\u00e7eri\u011fimizde .NET 8 ile C# programlama diline gelmi\u015f olan yeni koleksiyonel syntax yap\u0131s\u0131ndan bahsediyor olaca\u011f\u0131z. Malumunuz C#&#8217;ta bir koleksiyon ya da dizi olu\u015fturabilmek i\u00e7in genellikle a\u015fa\u011f\u0131daki s\u00f6z dizimlerinden istifade ediyorduk.<\/p>\n<div style=\"font-size:12px;\">\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nchar&#x5B;] charArray = new char&#x5B;3] { 'a', 'b', 'c' };\r\n<\/pre>\n<\/div>\n<div style=\"font-size:12px;\">\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nstring&#x5B;] stringArray = new string&#x5B;] { &quot;A&quot;, &quot;B&quot;, &quot;C&quot; };\r\n<\/pre>\n<\/div>\n<div style=\"font-size:12px;\">\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nList&lt;byte&gt; byteCollection = new() { 3, 5, 7 };\r\n<\/pre>\n<\/div>\n<p>Art\u0131k bu yap\u0131lar\u0131nda tan\u0131mlar\u0131n\u0131 <code>[<\/code> &#8211; <code>]<\/code> operat\u00f6rleri e\u015fli\u011finde daha pratik ve okunabilir bir \u015fekilde a\u015fa\u011f\u0131daki gibi uygulayabilmekteyiz;<\/p>\n<div style=\"font-size:12px;\">\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nchar&#x5B;] charArray = &#x5B;'a', 'b', 'c'];\r\n<\/pre>\n<\/div>\n<div style=\"font-size:12px;\">\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nstring&#x5B;] stringArray = &#x5B;&quot;A&quot;, &quot;B&quot;, &quot;C&quot;];\r\n<\/pre>\n<\/div>\n<div style=\"font-size:12px;\">\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nList&lt;byte&gt; byteCollection = &#x5B;3, 5, 7];\r\n<\/pre>\n<\/div>\n<p>G\u00f6r\u00fcld\u00fc\u011f\u00fc \u00fczere syntax a\u00e7\u0131s\u0131ndan kod maliyeti olduk\u00e7a azalm\u0131\u015ft\u0131r. \u015eimdi gelin bu yeni \u00f6zelli\u011fin, \u00f6zellikle yaz\u0131l\u0131m operasyonlar\u0131nda yo\u011fun bir \u015fekilde kullan\u0131lmalar\u0131 ve tercih edilmelerinden dolay\u0131 koleksiyonlar \u00fczerinde getirmi\u015f oldu\u011fu farkl\u0131 varyasyonlar\u0131 de\u011ferlendirelim;<\/p>\n<div style=\"font-size:12px;\">\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nclass MyClass\r\n{\r\n    ImmutableArray&lt;string&gt; names = &#x5B;&quot;H\u00fcseyin&quot;, &quot;R\u0131fk\u0131&quot;, &quot;\u015euayip&quot;, &quot;Bekir&quot;];\r\n\r\n    public IEnumerable&lt;int&gt; Numbers =&gt; &#x5B;3, 5, 6, 8, 5, 34, 23, 4, 26];\r\n\r\n    public int&#x5B;] GetNumbers() =&gt; &#x5B;3, 23, 5, 236, 35];\r\n}\r\n<\/pre>\n<\/div>\n<p>Ayr\u0131ca sabit de\u011ferlerin d\u0131\u015f\u0131nda bir diziyi ya da koleksiyonu a\u015fa\u011f\u0131daki gibi ilgili operat\u00f6rler e\u015fli\u011finde de\u011fi\u015fkenler ile de olu\u015fturabilmekteyiz;<\/p>\n<div style=\"font-size:12px;\">\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nstring hydrogen = &quot;H&quot;;\r\nstring helium = &quot;He&quot;;\r\nstring lithium = &quot;Li&quot;;\r\nstring beryllium = &quot;Be&quot;;\r\nstring boron = &quot;B&quot;;\r\nstring carbon = &quot;C&quot;;\r\nstring nitrogen = &quot;N&quot;;\r\nstring oxygen = &quot;O&quot;;\r\nstring fluorine = &quot;F&quot;;\r\nstring neon = &quot;Ne&quot;;\r\nstring&#x5B;] elements = &#x5B;hydrogen, helium, lithium, beryllium, boron, carbon, nitrogen, oxygen, fluorine, neon];\r\n<\/pre>\n<\/div>\n<p>T\u00fcm bunlar\u0131n d\u0131\u015f\u0131nda yeni syntax&#8217;\u0131n getirisi olarak birden fazla koleksiyonu ya da diziyi <strong><em style=\"color:red;\">Spread Element <code>(..)<\/code><\/em><\/strong> arac\u0131l\u0131\u011f\u0131yla tek bir koleksiyon ya da dizi de birle\u015ftirebilir ve a\u015fa\u011f\u0131daki \u00f6rnekte oldu\u011fu gibi bu birle\u015ftirme s\u00fcrecinde ekstradan da elemanlar ekleyebiliriz;<\/p>\n<div style=\"font-size:12px;\">\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nstring&#x5B;] vowels = &#x5B;&quot;a&quot;, &quot;e&quot;, &quot;i&quot;, &quot;o&quot;, &quot;u&quot;];\r\nstring&#x5B;] consonants = &#x5B;&quot;b&quot;, &quot;c&quot;, &quot;d&quot;, &quot;f&quot;, &quot;g&quot;, &quot;h&quot;, &quot;j&quot;, &quot;k&quot;, &quot;l&quot;, &quot;m&quot;,\r\n                       &quot;n&quot;, &quot;p&quot;, &quot;q&quot;, &quot;r&quot;, &quot;s&quot;, &quot;t&quot;, &quot;v&quot;, &quot;w&quot;, &quot;x&quot;, &quot;z&quot;];\r\nstring&#x5B;] alphabet = &#x5B;.. vowels, .. consonants, &quot;y&quot;];\r\n<\/pre>\n<\/div>\n<p>Compiler, <code>[<\/code> &#8211; <code>]<\/code> operat\u00f6rleriyle bildirilen bir koleksiyonu olu\u015fturman\u0131n en performansl\u0131 yolunu belirleyebilmek i\u00e7in statik bir analiz y\u00f6ntemi kullanmaktad\u0131r. Bunu \u015f\u00f6yle \u00f6rneklendirebiliriz. E\u011fer ki a\u015fa\u011f\u0131daki gibi <code>[]<\/code> ifadesiyle bo\u015f bir dizi tan\u0131ml\u0131yor olsayd\u0131k bu compiler a\u00e7\u0131s\u0131ndan <code>Array.Empty<int>()<\/code> olarak ger\u00e7ekle\u015ftirilecektir.<br \/>\nDavran\u0131\u015f;<\/p>\n<div style=\"font-size:12px;\">\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nint&#x5B;] numbers = &#x5B;];\r\n<\/pre>\n<\/div>\n<p>As\u0131l olan;<\/p>\n<div style=\"font-size:12px;\">\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nint&#x5B;] numbers = Array.Empty&lt;int&gt;();\r\n<\/pre>\n<\/div>\n<p>Velhas\u0131l kelam, <\/p>\n<p>.NET 8 ile gelmi\u015f olan collection expressions \u00f6zelli\u011fi koleksiyon tan\u0131mlama ve kullanma s\u00fcre\u00e7lerinde olduk\u00e7a ilgin\u00e7 bir kolayl\u0131k getirmekte ve daha da \u00f6nemlisi spread element davran\u0131\u015f\u0131 sayesinde koleksiyon t\u00fcrleri aras\u0131nda h\u0131zl\u0131ca ge\u00e7i\u015f yap\u0131lmas\u0131n\u0131 kolayla\u015ft\u0131rmaktad\u0131r. Ee art\u0131k bizlere d\u00fc\u015fen ihtiya\u00e7 noktalar\u0131nda bu \u00f6zellikten istifade ederek kodu daha sade ve dinamik tutmakt\u0131r \ud83d\ude09<\/p>\n<p>\u0130lgilenenlerin faydalanmas\u0131 dile\u011fiyle&#8230;<br \/>\nSonraki yaz\u0131lar\u0131mda g\u00f6r\u00fc\u015fmek \u00fczere&#8230;<br \/>\n\u0130yi \u00e7al\u0131\u015fmalar&#8230;<\/p>\n<!-- AddThis Advanced Settings generic via filter on the_content --><!-- AddThis Share Buttons generic via filter on the_content -->","protected":false},"excerpt":{"rendered":"<p>Merhaba, Bu i\u00e7eri\u011fimizde .NET 8 ile C# programlama diline gelmi\u015f olan yeni koleksiyonel syntax yap\u0131s\u0131ndan bahsediyor olaca\u011f\u0131z. Malumunuz C#&#8217;ta bir koleksiyon ya da dizi olu\u015fturabilmek i\u00e7in genellikle a\u015fa\u011f\u0131daki s\u00f6z dizimlerinden istifade ediyorduk. char&#x5B;] charArray&#46;&#46;&#46;<!-- AddThis Advanced Settings generic via filter on get_the_excerpt --><!-- AddThis Share Buttons generic via filter on get_the_excerpt --><\/p>\n","protected":false},"author":1,"featured_media":3014,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4912,12],"tags":[4915,5086,24,5087],"class_list":["post-27113","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-net-8","category-c-sharp-c","tag-net-8","tag-collection-expressions","tag-proje","tag-spread-element"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts\/27113","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/comments?post=27113"}],"version-history":[{"count":4,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts\/27113\/revisions"}],"predecessor-version":[{"id":27117,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/posts\/27113\/revisions\/27117"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/media\/3014"}],"wp:attachment":[{"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/media?parent=27113"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/categories?post=27113"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gencayyildiz.com\/blog\/wp-json\/wp\/v2\/tags?post=27113"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}