views:

390

answers:

4

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?

+1  A: 

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

CrazyJugglerDrummer
+6  A: 

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)

Chansik Im
+1  A: 

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.

Thorarin
A: 

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) ).

Inge Henriksen