I am replacing some very small bash scripts with Mono (C#). I have noticed some differing practices in C# tutorials online regarding convention. I'd like to know the reasoning behind them. If I should break this question up into multiple questions, let me know.

1) Use a namespace? I see that many tutorials don't bother with this, but some do. If the app is all contained in the Main and maybe a few other small functions, should I even bother with the namespace?

namespace SmallApp {
    class DoSomething {
        public static void Main(string[] args) {
            // code here;
        }
    }
}
class DoSomething {
    public static void Main(string[] args) {
        // code here;
    }
}

2) Public class? Some tutorials have the public modifier on the class, and some don't. I know that in Java the class must be marked as public, but in C# it seems to not matter. Your thoughts?

namespace SmallApp {
    public class DoSomething {
        public static void Main(string[] args) {
            // code here;
        }
    }
}
namespace SmallApp {
    class DoSomething {
        public static void Main(string[] args) {
            // code here;
        }
    }
}

3) Static Main? Some tutorials have the static modifier on the Main method, and some don't. I know that in Java the main method must be marked as static, but in C# it seems to not matter. Your thoughts?

namespace SmallApp {
    public class DoSomething {
        public static void Main(string[] args) {
            // code here;
        }
    }
}
namespace SmallApp {
    public class DoSomething {
        public void Main(string[] args) {
            // code here;
        }
    }
}

Thank you for your thoughts!

Recommended Answers

All 6 Replies

1) Whether or not you explicitly declare a namespace in a C# source file, the compiler adds a default namespace. This unnamed namespace, sometimes called the global namespace, is present in every file.

2) Does not matter.

3) It's a requirement that 'static' be there. If some tutorial left it off, then they are wrong :)

commented: Thanks, very informative. +2

1) Whether or not you explicitly declare a namespace in a C# source file, the compiler adds a default namespace. This unnamed namespace, sometimes called the global namespace, is present in every file.

Thanks. So long as I'm not using it explicitly, I won't bother adding a namespace to my source files.

2) Does not matter.

Thanks. That is what I thought.

3) It's a requirement that 'static' be there. If some tutorial left it off, then they are wrong :)

No, I was wrong! Going over my code, I see that, like with classes, the "public" modifier is sometimes left off (not "static" as I wrote in the OP). Should I understand that, like with classes, the "public" modifier is not important for the Main method?

Thanks.

The Main method is in fact always public. It always gets executed, it is the entrypoint of every C# application.

The Main method is in fact always public. It always gets executed, it is the entrypoint of every C# application.

Yes, of course! This is why I find it unusual that the "public" modifier can be eliminated. I agree that it is bad form.

Likewise, I think that it is bad form for the Main's class to not have a "public" modifier, but look at the default Visual Studio template for a C# console application:

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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
        }
    }
}

If it is bad form, then why does MS promote it?

Same reason they promote putting opening brace on a new line :)

And top posting in Outlook!

Same reason they promote putting opening brace on a new line :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.