C# Named Arguments

C# Named Arguments

Named arguments in C# allow you to specify the parameter name when calling a method. This improves readability and lets you change the order of arguments safely.


🔹 Basic Example


Normal Call


Using Named Arguments



🔹 Changing Argument Order


 


🔹 Mixing Positional and Named Arguments

✔ Positional arguments must come first.


❌ Invalid:



🔹 Named Arguments with Default Parameters



🔹 Named Arguments Improve Readability


✔ Easy to understand and maintain.


🔹 Named Arguments with Constructors


 


🔹 Common Mistakes

❌ Wrong parameter name
❌ Positional arguments after named
❌ Typos in parameter names


🔹 Summary

✔ Named arguments specify parameter names
✔ Argument order becomes flexible
✔ Works well with default values
✔ Improves code readability

You may also like...