C# Custom Methods with Parameters Example
C# custom methods with parameters provides an additional advantage to your coding style and increase the code re-usability. You may encounter some situations where you want to display a custom message to the end user. Yes, for this purpose you can also go for other options like Console WriteLine for a console application or display a message box for windows form application. But if you use a custom method, then it will reduce the amount of code you type and make your code more maintainable. The only thing which you have to keep in mind is that you must define a correct data type for your parameter and pass the same variable which is off same data type. If you would not follow this condition, then your application would not compile. If you will further increase the number of parameters in your custom method based on the requirements, then you can pass multiple parameters. A very simple C# custom methods with parameters example is given below where we are making use of same approach to pass a custom message.
using System; namespace comments { class Program { static void Main(string[] args) { PrintMessage("Hello World"); } public static void PrintMessage(string message) { Console.WriteLine(message); } } }
Output:
Hello World
Press any key to continue . . .