About
In this code snippet, we will learn about methods in C#.
A method is basically a code block with a name. It can have input parameters to pass data into the code block and it can have a return type to return data out of the code block. A method can be called by writing its name followed by a set of parentheses.
Methods can be designated as static meaning you don’t have to make an instance of the method’s class to use it. They also have an access modifier(public, private, …) which determines who that method can be called or invoked by.
Methods allow us to break code up into smaller code blocks that only perform one or a couple functions. Very importantly methods allow us to reuse code and thus make it easier to understand and maintain. For example, if we have to fix a bug or add a feature we only have to do it once in the method.
Let’s see how to implement and use a method in the code below.
Code:
[amp-gist id=”eecb52b12236526eddfb2bd60dae89e5″]