Call the Method

A method is a basic part of a program. It can solve a certain problem, eventually, take parameters and return a result. Whenever a program has been created, it is always a good practice to use methods, as a way to make your code better structured and easy to read, hence to be maintained by other people. It also restricts us from repeating a code again and again. In C# there are some pre-defined methods which we use to ease our coding e.g. –  Console.WriteLine(). Here WriteLine is a method. Let’s take an example of a custom method where we will reverse the input string.


using System;
namespace HelperMethods
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("The Name Game");
Console.Write("What's your first name? ");
string firstName = Console.ReadLine();
Console.Write("What's your last name? ");
string lastName = Console.ReadLine();
Console.Write("In what city were you born?");
string city = Console.ReadLine();
DisplayResult(ReverseString(firstName),
ReverseString(lastName),
ReverseString(city));
Console.ReadLine();
}
private static string ReverseString(string message)
{
char[] messageArray = message.ToCharArray();
Array.Reverse(messageArray);
return String.Concat(messageArray);
}
private static void DisplayResult(
string reversedFirstName,
string reversedLastName,
string reversedCity)
{
Console.Write("Results: ");
Console.Write(String.Format("{0} {1} {2}",
reversedFirstName,
reversedLastName,
reversedCity));
}
private static void DisplayResult(string message)
{
Console.Write("Results: ");
Console.Write(message);
}
}
}

 


Here we are asking the user to input their name and we are displaying the result in reverse order. We first create the ReverseString() method and then two DisplayResult() method. Here we are defining ReverseString() method with private static string and Display by public static void. But why? What are these keywords do?
Well, public or private are access modifiers. If a method is declared by access modifier private, it cannot be called from anywhere, except the class in which it is declared and in case of public it can be accessed by anywhere. By default, if no access specifier is given method will be public. When a method has a keyword static, in its declaration, this method is called static. To call a static method there is no need to have an instance of a class in which the static method is declared. And the last one is the return type of the method i.e. what data type it will return when called. String means it will return a string and void means it will return nothing.
We have two DisplayResult method. But there is a difference in signature of those. In object-oriented programming, a method is identified by name of the method, and list of parameters. These two elements define the method signature. We are defining one method twice with different signature to give user an option of choosing whichever version they want.

Leave a Reply

Your email address will not be published. Required fields are marked *