0

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);
    }
}
Sandeshpd
  • 21
  • 5
  • Does this answer your question? [How to define a method following top-level statements](https://stackoverflow.com/questions/72160360/how-to-define-a-method-following-top-level-statements) – shingo May 30 '22 at 12:04

2 Answers2

3

There are two important things to understand here:

Firstly, top-level statements and implicit using directives are entirely different features. So you can declare a class as normal, but still omit the using directives. Likewise if you don't want to declare a namespace, you don't have to - although these days you could just use a file-scoped namespace declaration, e.g. namespace MyNamespace; without indenting everything.

Secondly, even with top-level statements, you can write methods... but they're effectively local methods within the Main method, and come with all the restrictions that imposes (e.g. no overloading). Note that even with top-level statements for your entry point, you can still declare other classes elsewhere - it would be entirely reasonable to have a brief Program.cs using top-level statements just to get everything going, but use "regular" C# elsewhere in the project. Indeed, that's how the ASP.NET Core templates work these days.

As an example, given the sample code, you could write (slightly amended for naming and to avoid warnings):

System.Console.WriteLine("Enter the word");
string? input = Console.ReadLine();
if (input is null)
{
    Console.WriteLine("No input");
    return;
}
System.Console.WriteLine($"Original string: {input}");
System.Console.WriteLine($"Middle character of {input} is {GetMiddle(input)}");

string GetMiddle(string input)
{
    int i = 1 - input.Length % 2;
    return input.Substring(input.Length / 2 - i, i + 1);
}

In this particular case you don't actually need the parameter for the method - input would be captured anyway, because GetMiddle is a local method.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Does it mean I'm required to define ```Main()``` method in order to define and use separate method outside ```Main()```? Otherwise it will not work, am I right? – Sandeshpd May 30 '22 at 12:33
  • @Sandeshpd: I'd need to see a concrete example of *exactly* what you mean. You can introduce additional methods in the same (implicit) class as the top-level statements, and they're effectively local methods. If you introduce separate classes etc in different files, you can use those from top-level statements with no problems. (It's not clear what you've tried at this stage - it would be much easier to help you if you'd give a concrete example of what you're trying to do and what happened when you tried it.) – Jon Skeet May 30 '22 at 12:51
  • I have provided the code. Please see above, I am kind of new to StackOverflow. I highly regret of any inconvenience caused to you. – Sandeshpd May 31 '22 at 05:37
  • @Sandeshpd: I've added a sample - but please move your code into the *question*. – Jon Skeet May 31 '22 at 05:59
0

You can write that like a normal method.

using System;
Console.WriteLine("Hello World");
display();

static void display()
{
    Console.WriteLine("from display");
}
SDHEER
  • 46
  • 4
  • But I don't want to include ```using System;``` namespace, unless it is needed. In fact, current language feature is much useful. Much less clutter, focuses just on the executable section of the code. – Sandeshpd May 30 '22 at 12:48
  • Well you can remove that part too. it will still work. for the console to print you may modify it like Sytem.Console.WriteLine – SDHEER May 30 '22 at 13:05