C#:- Compiler Error – “The entry point of the program is global code; ignoring ‘Main(string[])’ entry point”

Background

Let us go with our old stand by.

HelloWorld.

 

Language

Language – C

Script

Code


#include <stdio.h>;
int main() 
{
   // printf() displays the string inside quotation
   printf("Hello, World!");

   return 0;
}
         

Output

Output Image

Using tio.run c / gcc compiler

Output – Text

Hello, World!        

 

Language – C#

C# – Without Class Definition

Outline

Let us convert our c code to C#.

Code Changes
  1. Import
    • c:-
      • #include <stdio.h>
        • Include stdio.h header file
    • c#:-
      • using system;
        • Import Types from System namespace
  2. Display Output
    • c:-
      • println
    • c#:-
      • Console.WriteLine

Code


using System;
    
static void Main(string[] args)
{
    
    Console.WriteLine("Hello World");
    
}
           

Compile

Compiler – Version – Visual C# Compiler Version 3.11

setlocal

    if not exist bin mkdir bin
    
    rem compile helloWorld.non.class.cs
    csc helloWorld.non.class.cs -out:binhelloWorld.non.class.exe

endlocal

Compile – Output – Image

Compile – Output – Text

helloWorld.non.class.cs(3,13): warning CS7022: The entry point of the program is global code; ignoring 'Main(string[])' entry point.
helloWorld.non.class.cs(3,13): warning CS8321: The local function 'Main' is declared but never used      

 

Run Binary

bin\helloWorld.non.class.exe

Output

Output – Image

Output – Text

bin\helloWorld.non.class.exe

Explanation

The code did not display helloWorld.

 

Compiler – Version – TIO – Build Engine version 16.3.0-ci for Mono
Compile – Output – Image

Compile – Output – Text
Microsoft (R) Build Engine version 16.3.0-ci for Mono
Copyright (C) Microsoft Corporation. All rights reserved.

Program.cs(3,13): error CS0116: A namespace cannot directly contain members such as fields or methods [/home/runner/TIO-CS.csproj]
Cannot open assembly 'bin/TIO.exe': No such file or directory.
Explanation

Using the online tool, TIO Run, we received an error message.

The message reads:-

Program.cs(3,13): error CS0116: A namespace cannot directly contain members such as fields or methods [/home/runner/TIO-CS.csproj]

Quick Explanation

  1. Program.cs ( 3,13)
    • Our source code is placed in file ( Program.cs )
    • The error is sourced to line 3
  2. TIO-CS.csproj
    • A project file ( TIO-CS.csproj ) is built
    • The source file ( program.cs ) is referenced within the project file
  3. Error Statement
    • A namespace cannot directly contain members such as fields or methods
      • Not sure, but a namespace skeleton might very well have been added to the tailored source code file

 

 

C# – With Class Definition

Outline

Let us convert our c code to C#.

We will go one step further, place our code in a class.

Code Changes
  1. Class
    • We will enclose our code in a class garment

Code

           
using System;

class helloWorld
{
    
    static void Main(string[] args)
    {
        
        Console.WriteLine("Hello World");
        
    }
             
}

Compile


setlocal

    if not exist bin mkdir bin
    
    rem compile helloWorld.in.class.cs
    csc helloWorld.in.class.cs -out:binhelloWorld.in.class.exe

endlocal

Compile – Output – Image

Compile – Output – Text

csc helloWorld.in.class.cs -out:binhelloWorld.in.class.exe
Microsoft (R) Visual C# Compiler version 3.11.0-4.21403.6 (ae1fff34)
Copyright (C) Microsoft Corporation. All rights reserved.   

 

Run Binary


bin\helloWorld.in.class.exe

Output
Output – Image

Output – Text

bin\helloWorld.in.class.exe

Explanation

The code displays “Hello World“.

 

C# – Top Level Statement

Outline

C# version 9 introduces top level statements.

Inquire

csc
Code

Let us determine the c# language versions supported by our installed compiler.


csc /langversion:?
         
Output – Image

Output – Text

csc /langversion:?
Supported language versions:
default
1
2
3
4
5
6
7.0
7.1
7.2
7.3
8.0
9.0 (default)
latestmajor
preview
latest   
      
Explanation
  1. Our current language version is 9
  2. Version 9 is also our default version

Code

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

Compile

Compile – Language Version 9
Outline
  1. We will use the /langversion switch to request c# language version 9
    • Syntax
      • /langversion:[version]
    • Sample
      • /langversion:9

setlocal

    if not exist bin mkdir bin
    
    rem compile helloWorld.topLevelStatement.cs against language version 9
    
    csc helloWorld.topLevelStatement.cs -out:binhelloWorld.topLevelStatement.exe /langversion:9
    
endlocal


Compile – Output – Image – Language Version 9

 

Compile – Output – Text – Language Version 9

compile.topLevelStatement.language.version.9.cmd

setlocal

if not exist bin mkdir bin

rem compile helloWorld.topLevelStatement.cs against language version 9

csc helloWorld.topLevelStatement.cs -out:binhelloWorld.topLevelStatement.exe /langversion:9
Microsoft (R) Visual C# Compiler version 3.11.0-4.21403.6 (ae1fff34)
Copyright (C) Microsoft Corporation. All rights reserved.

endlocal

Output

Output – Image

 

Compile – Language Version 8
Outline
  1. We will use the /langversion switch to request c# language version 8
    • Syntax
      • /langversion:[version]
    • Sample
      • /langversion:8

setlocal

    if not exist bin mkdir bin
    
    rem compile helloWorld.topLevelStatement.cs against language version 8
    
    csc helloWorld.topLevelStatement.cs -out:binhelloWorld.topLevelStatement.exe /langversion:8
    
endlocal


Compile – Output – Image – Language Version 8

 

Compile – Output – Text – Language Version 8
compile.topLevelStatement.language.version.8.cmd

setlocal

if not exist bin mkdir bin

rem compile helloWorld.topLevelStatement.cs against language version 8

csc helloWorld.topLevelStatement.cs -out:binhelloWorld.topLevelStatement.exe /langversion:8
Microsoft (R) Visual C# Compiler version 3.11.0-4.21403.6 (ae1fff34)
Copyright (C) Microsoft Corporation. All rights reserved.

helloWorld.topLevelStatement.cs(3,1): error CS8400: Feature 'top-level statements' is not available in C# 8.0. Please use language version 9.0 or greater.

endlocal

Compile – Explanation

Thanks Goodness.

The error message is to the point.

It reads:-

error CS8400: Feature ‘top-level statements’ is not available in C# 8.0. Please use language version 9.0 or greater.

 

Source Code Control

GitHub

DanielAdeniji/helloWorldInCSharp

Link

Tools

TIO ( TryItOnline )

In this post we used TIO ( TryItOnline ) to try out different programming languages, environments, and options.

A quick read up of TIO is availed at https://tryitonline.net/.

It’s service is availed at https://tio.run/#.

 

References

  1. Microsoft
    • .Net
      • Docs > .NET > C# guide > Fundamentals
        • Top-level statements – programs without Main methods
          Link

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s