-6

I'm beginning to learn C# and I come from a C++ background. The example page I was supposed to create by these instructions looks like

using System.Web;
using System.Web.Mvc;

namespace MvcMovie.Controllers
{
    public class HelloWorldController : Controller
    {
        // 
        // GET: /HelloWorld/ 

        public string Index()
        {
            return "This is my <b>default</b> action...";
        }

        // 
        // GET: /HelloWorld/Welcome/ 

        public string Welcome()
        {
            return "This is the Welcome action method...";
        }
    }
}

My main question is why the HelloWorldController class is prefixed by public. I understand that HelloWorldController is derived from Controller, but why does a class need to be public in the first place? My understanding of the words public and private is that they only have meaning if they're functions inside a class, and that public are the ones that can be used by instances of that class. Also, where is my main.cs in this Visual Studio ASP.NET MVC project that I created?

Ben
  • 2,433
  • 5
  • 39
  • 69
  • 5
    This answer is arguably best served by a good _Learning c# 101_ book or online tutorial rather than a question for SO? –  May 06 '15 at 00:22
  • C# has exactly the same meaning for `public` as C++ in this context. It is very unclear what "public and private is that they only have meaning if they're functions inside a class" mean in C++ or C# – Alexei Levenkov May 06 '15 at 00:23
  • 3
    msdn is great: https://msdn.microsoft.com/en-us/library/x9afc042.aspx – Dyrandz Famador May 06 '15 at 00:23
  • 1
    I Agree with @MickyDuncan. But to help you out: ASP.NET MVC apps don't have (or need) an main.cs. The framework takes care of when things are called and thus isn't like a normal application. – OneHoopyFrood May 06 '15 at 00:24
  • 2
    @AlexeiLevenkov: `public` qualifier on a class has no meaning in C++, so I don't understand what you mean by `same meaning`. I believe the difference is the compilation method. C++ has header files of course. – Jesse Good May 06 '15 at 00:29
  • @JesseGood Looks like Alexei didn't fully understand the question. His answer is correct if asking about public/private on a method. – OneHoopyFrood May 06 '15 at 00:33
  • @JesseGood fair point... should be something like "when `public` applies to a C++ class (nested class in public section of parent class) than meaning is *close* to meaning in C#, also unlike C++ in C# access modifiers applied to individual classes/methods/fields". Clearly my comment was not exactly the best example of comments... – Alexei Levenkov May 06 '15 at 02:12

3 Answers3

4

The purpose of public and private on a class differs from that on methods.

Classes (C# Programming Guide)

public class Customer
{
    //Fields, properties, methods and events go here...
}

The class keyword is preceded by the access level. Because public is used in this case, anyone can create objects from this class.

Access Modifiers (C# Programming Guide)

public class Bicycle
{
    public void Pedal() { }
}

The type or member can be accessed by any other code in the same assembly or another assembly that references it.

Dave Anderson
  • 11,836
  • 3
  • 58
  • 79
  • 1
    I believe the namespace has a part to play? i.e. If I were to call the `Customer` class from another namespace, that would require the class to be public. – Ben May 06 '15 at 00:27
  • 3
    @Ben Namespaces have no role in accessibility. They are merely for logical organization. Assemblies, on the other hand, do play a role. Since assemblies will typically encompass a single namespace, they can be easily confused. – Mike Zboray May 06 '15 at 00:31
  • 2
    I realize these are quotes from MSDN but the wording in the first one is misleading. You can create instances of `Customer` because it is also given a public constructor by default, not solely because the class itself is declared public. At the language level, access modifiers control one thing: who can reference a give program element by its name. – Mike Zboray May 06 '15 at 00:39
1

A private class wouldn't be able to be used by anything, unless it were within another class. C# doesn't allow un-nested classes to be private, as nothing could use it.

However, there is another option: you could mark the class as internal instead. internal restricts access to within the current assembly.

OneHoopyFrood
  • 3,829
  • 3
  • 24
  • 39
user2588666
  • 821
  • 1
  • 7
  • 16
1

The keyword indicates who is allowed to create instances (objects) from this class. Private would be used if you have classes nested inside each other, and you don't won't it accessible from outside the class.

From MSDN

The class keyword is preceded by the access level. Because public is used in this case, anyone can create objects from this class. The name of the class follows the class keyword. The remainder of the definition is the class body, where the behaviour and data are defined. Fields, properties, methods, and events on a class are collectively referred to as class members.

Ben
  • 2,433
  • 5
  • 39
  • 69