𝐀𝐝𝐝𝐑𝐚𝐧𝐠𝐞() 𝐭𝐨 𝐜𝐨𝐦𝐛𝐢𝐧𝐞 𝐜𝐨𝐥𝐥𝐞𝐜𝐭𝐢𝐨𝐧𝐬?

 𝐒𝐭𝐢𝐥𝐥 𝐮𝐬𝐢𝐧𝐠 𝐀𝐝𝐝𝐑𝐚𝐧𝐠𝐞() 𝐭𝐨 𝐜𝐨𝐦𝐛𝐢𝐧𝐞 𝐜𝐨𝐥𝐥𝐞𝐜𝐭𝐢𝐨𝐧𝐬?


C# 12 gives us a cleaner alternative with 𝐂𝐨𝐥𝐥𝐞𝐜𝐭𝐢𝐨𝐧 𝐄𝐱𝐩𝐫𝐞𝐬𝐬𝐢𝐨𝐧𝐬.

Instead of creating a list and then calling AddRange(), you can create the final collection in a single expression.




𝐓𝐢𝐧𝐲 𝐍𝐨𝐭𝐞𝐬

✅ AddRange() modifies an existing list.
✅ Collection Expressions create a 𝐧𝐞𝐰 𝐜𝐨𝐥𝐥𝐞𝐜𝐭𝐢𝐨𝐧.
✅ Great for cleaner, more readable code.
✅ Introduced in 𝐂# 12 (.𝐍𝐄𝐓 8).

Small language features like this make your code easier to read, review, and maintain.

Which approach do you prefer?

AddRange() or Collection Expressions?

Comments

Popular posts from this blog

𝗙𝗹𝘂𝗲𝗻𝘁𝗩𝗮𝗹𝗶𝗱𝗮𝘁𝗶𝗼𝗻 𝗶𝗻 𝗔𝗦𝗣.𝗡𝗘𝗧 𝗖𝗼𝗿𝗲 - 𝗖𝗹𝗲𝗮𝗻, 𝗙𝗹𝗲𝘅𝗶𝗯𝗹𝗲 𝗠𝗼𝗱𝗲𝗹 𝗩𝗮𝗹𝗶𝗱𝗮𝘁𝗶𝗼𝗻 𝗳𝗼𝗿 𝗠𝗼𝗱𝗲𝗿𝗻 .𝗡𝗘𝗧 𝗔𝗽𝗽𝘀

Performance Optimization in Sitecore

Azure Event Grid Sample code