Question

I have a program of some length, with a class and many methods. All of these have to be contained in one huge class that wraps the entire file except the using statements. The necessity of that huge wrapping class seems pointless, is this used only later when making a program with multiple files?

    using System;
    using System.IO;
    using System.Text.RegularExpressions;
    using System.Collections.Generic;
    using System.Linq;

    public class CollectKeywordsFromLogs // why do I need this ( when it is the only class [as i just added the test class for the question])
    {

    //a class
        //constructor
        //methods

    //methods

    public static void Main()  // and why would i need to explicitly declare this public? it works either way, and a private main would be useless no?
    {}

    }

this is my full file. i compile csc file.cs then file.exe.

oh duh, case sensitive. thanks. but still -> why do i need the wrapping class when nott using the test class?

Was it helpful?

Solution

There are no global methods or variables in C#. They must be inside of a type declaration such as class or struct. This applies to Main() method as well.

Eric Lippert posted a nice answer to SO question, Why C# is not allowing non-member functions like C++, and followed up further in his blog: Why Doesn't C# Implement "Top Level" Methods?

You also may find the following MSDN link useful: Comparison Between C++ and C#

The Main() method is an entry point of C# programs. There should be only one entry point in a program, yet, there can be multiple classes that contains a Main() method in a program. However, if you have multiple Main methods, you should specify which Main method to be used as an entry point by compiling your program with the compiler option /main.

The Main() method must be static and should not be public according to the MSDN. By default, VS2008 creates a Main() as static private and the runtime can call it without any issue. You can change it to public and it will compile fine. However, I think it is really awkward since you cannot prevent other classes from calling into Main() as it is public.

For more information on Main method, you can see the following MSDN article: Main() and Command-Line Arguments (C# Programming Guide)

OTHER TIPS

In C# it should be Main <---capital M :D

It seems to me you're using a class, when you should be using a namespace.

Other than that... in C#, all code has to be inside a class of some kind. It would be silly to make an exception for the Main() method.

A private Main() actually works fine as a program entry point, by the way.

A Main has to have the right signature to be recognized as Program startup method. That means you can have only 1 per class. You can have more than 1 per program, you can select the class that should provide the main either on the compiler command line or in Project|Properties|Application (look for the combobox 'Startup Object').

That big class sounds strange, can you show the outline of what you mean? An to take a guess, aren't you confusing class and namespace here?

Add

In response to your Edit, in C# every method, including Main, must be in a class. See this post. And the signature for main, by definition, is

public static void Main(string[] args) { }

Although you are allowed to omit the args parameter. The command line in your case would be:

csc /main:CollectKeywordsFromLogs  file.c

There are default rule rules when you leave out /main

C# uses the class-based object-oriented programming paradigm, global variables belong to other paradigms like procedural programming; have a look here for an article on programming paradigms. True, some object-oriented programming languages, espesially C++, supports global variables, but not C# (and thats a good thiing imo :o) ).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top