Since the .NET version has been updated, there is no need to write entire C# code just to print 'Hello World!'. Just Console.WriteLine();
is enough. But what I could not understand, is how to define a new function outside Main()
?
If I want to develop a separate function outside Main()
, do I need to write entire code including using System;
and namespaces and Main()
function? I am talking about updated version which is C# v10.
Please, do enlighten me!
I hope this question is not much lengthy and you got idea regarding the problem.
Here is the code for your reference:
class Program
{
public static void Main(string[] args)
{
string? theWord;
System.Console.WriteLine("Enter the word");
theWord = Console.ReadLine();
System.Console.WriteLine("original string: " + theWord);
System.Console.WriteLine("middle character of " + theWord + " is " + test(theWord));
}
public static string test(string theWord)
{
int i = 1 - theWord.Length % 2;
return theWord.Substring(theWord.Length / 2-i, 1+i);
}
}