Whats the difference between "Class" and "Private Class" declaration in C#? [duplicate] - c#

This question already has answers here:
Is it possible to have a private class?
(4 answers)
Closed 9 years ago.
Whats the difference between "Class" only and "Private Class" declaration in C#?

If these are nested classes, there's no difference:
namespace Foo
{
public class Outer
{
private class ExplicitlyPrivate {}
class ImplicitlyPrivate {}
}
}
Type members always default to being private.
If it's a top-level class, then you can't make it private - but the default is internal:
namespace Foo
{
class ClassIsInternalByDefault {}
}

When you declare a class without specifying an accessibility modifier it will default to the lowest accessibility possible.
More practically, specifying private when private is not permissible can result in a compilation error.

A simple answer is to say that a private class is meant to protect attributes within that class from being changed by any external classes, other than during construction of the program. A normal "class", well, doesn't have that protection.
It is a form of ENCAPSULATION.

Related

C# partial classes: are non-static field initializations across executed before the constructor? [duplicate]

This question already has answers here:
Is the "textual order" across partial classes formally defined?
(2 answers)
Closed 1 year ago.
Lots of answers for similar questions seem to always be about static classes. This is about instance fields.
Suppose I have this
// File.cs
public partial class Stuff
{
public Stuff()
{
obj.DoSomething();
}
}
// OtherFile.cs
public partial class Stuff
{
MyObject obj = new MyObject("test");
}
Is this guaranteed to be safe? Like it won't do, for example, field initialization and run the constructor in one class, and then do the field initialization in the other part?
Does the C# language guarantee this will always be safe?
Note: Is the "textual order" across partial classes formally defined? does not answer the question. The answer for the current question gives an answer that is not part of such a question. It should not be linked as a duplicate to this particular link.
Yes, because partial classes are still compiled into one class. The partial syntax feature just lets you split the definition of a class into multiple files. There are no "parts" as far as the compiled type is concerned.
So all sequencing regarding static and non-static constructors, initializers, etc. still hold.

About protected and private [duplicate]

This question already has answers here:
In C#, what is the difference between public, private, protected, and having no access modifier?
(19 answers)
Closed 3 years ago.
I am new to C# programming and I have some questions about access modifier protected and private .
Is it true that when data members and member functions of the class are kept "private" ,they can only be accessed by that class and can't even be accessed by the child (derived) class of that class?
In case of protected, if a data members and member functions of that class are kept protected ,then only the code of that class can access those data members and member functions and also if that class has a derived(child ) class ,then even child class can also access the protected data members and member functions of that base (parent ) class ?
Thank
Protected members are accessible by subclasses, private are accessible only in the class
Well this is going to be a short answer but : yes, this is correct.

I don't understand why a class is "public" [duplicate]

This question already has answers here:
In C#, what is the difference between public, private, protected, and having no access modifier?
(19 answers)
Closed 7 years ago.
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?
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.
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.
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.

Why is it not possible to inherit a public class from an internal class? [duplicate]

This question already has answers here:
Why can't my public class extend an internal class?
(5 answers)
Closed 9 years ago.
See this code:
internal class c
{
private int d;
}
public class a : c
{
private int b;
}
Why can I not inherit a public class from an internal class? Why does the compiler have this behavior?
Because the public class would be visible outside your current assembly, while the internal one isn't. When deriving from a class you can only restrict visibility further, because in your case it would make the implementation of c available to consumers outside your assembly which kind of defeats the purpose of making the class internal in the first place.
You can use composition instead of inheritance, though.
C# design principle. Derived class should atleast have same accessibility as the parent class. In your case it is not hence not allowed. Take a look at Eric Lippert's view on this deriving public class from an internal class
Because "public class" is more "visible" than "internal class".
C# language has visibility protection layer that prevents this.
Internal classes can only be accessed from within the Assembly in which they are defined. When public class a inherits from an internal class in effect attempts to make the internal class public.
To avoid this encapsulate the internal class in the public class.

Protected Internal Member [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What is the difference between ‘protected’ and ‘protected internal’?
I have seen a lot of controversy over the true meaning of declaring a member protected internal.
Under this context is the member's access modifier either "protected or internal" or "protected and internal"?
The documentation is clear that it is "protected or internal".
That is - a member is accessible both within the assembly and any subtype.
protected internal
The type or member can be accessed by any code in the assembly in which it is declared, or from within a derived class in another assembly. Access from another assembly must take place within a class declaration that derives from the class in which the protected internal element is declared, and it must take place through an instance of the derived class type.

Categories