𝐀𝐝𝐝𝐑𝐚𝐧𝐠𝐞() 𝐭𝐨 𝐜𝐨𝐦𝐛𝐢𝐧𝐞 𝐜𝐨𝐥𝐥𝐞𝐜𝐭𝐢𝐨𝐧𝐬?
𝐒𝐭𝐢𝐥𝐥 𝐮𝐬𝐢𝐧𝐠 𝐀𝐝𝐝𝐑𝐚𝐧𝐠𝐞() 𝐭𝐨 𝐜𝐨𝐦𝐛𝐢𝐧𝐞 𝐜𝐨𝐥𝐥𝐞𝐜𝐭𝐢𝐨𝐧𝐬?
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
Post a Comment