C# Methods

C# Methods

A method in C# is a block of code that runs only when it is called. Methods are used to perform actions, return values, and reuse code, making programs cleaner and easier to maintain.


🔹 Method Syntax



 


🔹 Simple Method (No Return, No Parameters)



 

Calling the Method



 


🔹 Method with Parameters



 


🔹 Method with Return Value



 


🔹 Method with Multiple Parameters



 


🔹 Method Overloading

Multiple methods with the same name but different parameters.


 


🔹 Optional Parameters



 


🔹 Named Arguments


 


🔹 Pass by Value



 

✔ Original value unchanged.


🔹 Pass by Reference (ref)



 

✔ Original value changes.


🔹 out Parameters



 


🔹 Recursive Method


 


🔹 Common Mistakes

❌ Forgetting static in Main context
❌ Missing return statement
❌ Parameter mismatch


🔹 Summary

✔ Methods improve code reusability
✔ Can return values or be void
✔ Support parameters & overloading
ref and out modify values

You may also like...