Parameterless struct constructors, which so far have been prohibited in C#, are now implemented in the Visual Studio 14 CTP (CTP 4 at the time of writing) as an experimental feature.
However, such parameterless constructors must be public. You cannot make them internal or use any other access modifier.
In the C# Design Notes for Aug 27, 2014, I have found an explanation for this:
C#, VB and F# will all call an accessible parameterless constructor if they find one. If there is one, but it is not accessible, C# and VB will backfill default(T) instead. (F# will complain.)
It is problematic to have successful but different behavior of new S() depending on where you are in the code. To minimize this issue, we should make it so that explicit parameterless constructors have to be public. That way, if you want to replace the “default behavior” you do it everywhere.
What is meant by "depending on where you are in the code", and how does enforcing public parameterless constructors solve the issue?
Where you are in the code means that you will get different behaviors depending on the location of the code that uses a struct.
For instance, if you have a parameterless constructor that's marked as internal, the behavior varies if you create an instance from the same assembly in which the struct is defined or from another assembly.
In first case the parameterless constructor is called because its accessible and in second case default(T) is called. Same situation is enountered for private and protected constructors.
For example, suppose we have two assemblies, A and B:
//Assembly A
public struct SomeStruct
{
public int x = 0;
internal SomeStruct()
{
x = 10;
}
}
public static void DoSomething()
{
var someStruct = new SomeStruct();
Console.WriteLine(someStruct.x); // prints 10
}
//Assembly B
public static void DoAnotherThing()
{
var someStruct = new SomeStruct();
Console.WriteLine(someStruct.x); // prints 0
}
This leads to inconsistent behavior depending on where you are in the code and the reason why they forced using public constructors.
You can be "in a place in the code" in the following meaning:
in the context of your struct, i.e.:
struct X
{
//...
public X CreateNew() { return new X(); }
}
in the context of your assembly:
new X(); // from the same assembly
in another assembly
new X(); // from another assembly
So, the mechanism you speak of, would have to behave differently for exactly the same piece of code (new X();), in each of these contexts. In the first example, you can use the constructor regardless of its access modifier. In the second example, you can access one if it's public or internal. In the third one, it could've been used only if it's public.
Making it public simplifies the situation, as it will be accessible in all of these contexts.
I have a mainForm variable that is used nearly everywhere in my program. I'd like to be able to reference to this variable without having to type the name of the class that holds this variable (i.e. instead of Global._mainForm, I should be able to just type _mainForm to access this variable anywhere within the same namespace).
What is the best way to accomplish this?
Anytime I see a global anything I get worried, so I would reconsider how your program is architected. You can almost always get away without having a globally accessible object. Although there are legitimate reasons to have a globally accessible object, usually I try to stay away from them.
From the little information you've given, you might be able to use events to talk to the main window instead of directly accessing it.
If you do have real need for a globally accessible object then you could do something like this
Note: In the code below everything is static, conversely you could create a special static class that holds a reference to the instance of the class you're really interested in. But there is no way that I'm aware of to hold a global reference that is outside of all namespaces to an instance of an object.
using Bar = ProbablyAReallyBadIdeaToHaveAGlobalAnythingButHeyWhyNot.TestClass;
namespace ProbablyAReallyBadIdeaToHaveAGlobalAnythingButHeyWhyNot
{
public static class TestClass
{
public static int TestFoo { get; set; }
}
}
namespace Foo.SomeOtherNamespace
{
class MyClassThatDoesStuff
{
public void DoStuff()
{
Bar.TestFoo = 123;
}
}
}
You can't accomplish this in C#.
Namespaces are simply part of the type name of a class. Lexically, you can think of a namespace as containing only other namespaces and classes.
Classes may contain fields, functions, events, properties, nested classes, etc., but not nested namespaces.
Therefore there is no way to to define a symbol in a namespace outside of class that aliases or refers to a symbol contained in a specific class.
When resolving the reference to the variable name _mainForm, the only place that it does not require additional qualification is from within the scope of the class that it is defined in.
If you are in a namespace scope that is the same as as the class that defines _mainForm (and _mainForm is a static instance), then you only need access it by qualifying it with the class name. Otherwise you will require additional namespace qualification, or perhaps trickery with a using directive. But outside of the containing class, you will always require a qualification of some kind to access _mainForm.
See http://en.csharp-online.net/ECMA-334:_10.7_Scopes for excruciating detail.
The only way I can see, in C#, for every class in a namespace to be able to directly address the same variable is by defining the variable as static in a base class used by all other classes in the namespace. Since static variables can be addressed as local variables within the class (or classes inheriting from) they are defined in, the variable will be available without class qualifications.
Can't say it is a good idea, but that's not what you asked ...
I want to create a class outside a namespace so that its default access modifier is 'PRIVATE'. I am doing like this:
namespace KnowStructs
{
class Clas1 {}
}
class MyClass {}
But still my class 'MyClass' is referred as Internal when I look in Reflector.
Can anyone help me out.
From Accessibility Levels:
Top-level types, which are not nested in other types, can only have internal or public accessibility. The default accessibility for these types is internal.
and:
Access modifiers are not allowed on namespaces. Namespaces have no access restrictions.
and for private:
Private members are accessible only within the body of the class or the struct in which they are declared
That is, the private keyword is explicitly defined in terms of a containing class or struct.
So whatever you're trying to do, I don't understand it. How could a top level private type possibly be useful? No other code would be able to reference it (in any way, even if it had e.g. static factory methods).
If a private class is allowed that is not a nested type then what would that mean? If it is more restrictive than internal then how would you use it or create an instance. Any use case will require it to be internal at a minimum. I would like to see how you intend to use it.
It simply makes no logical sense.
Whereas having a private nested class scopes itself to the parent containing class. If it were internal then you still will be able to make an instance within the assembly.
So for classes having no modifier is internal by default for non nested types and private for nested types as .Net always applies the most restrictive access when no modifier is specified.
You can make the class internal, if you only want to be accessible by classes in your namespace
If I declare an interface, or a type as private in one file, is it private to that file or the namespace?
The compiler generates an error for File2: 'Error 14 Inconsistent accessibility: parameter type 'DIDemo1.IImageRepository' is less accessible than method 'DIDemo1.ImageGetter.ImageGetter(DIDemo1.IImageRepository)'
What I don't understand is that MyClass can use the interface but ImageGetter class cannot.
File1:
namespace DIDemo1 {
interface IImageRepository {
Image[] GetImages();
}
public class MyClass : IImageRepository {
#region IImageRepository Members
public Image[] GetImages() {
return new Image[] { };
}
#endregion
}
}
File2:
namespace DIDemo1 {
public class ImageGetter {
IImageRepository _repo;
public ImageGetter(IImageRepository repository) {
_repo = repository;
}
public Image[] GetImages() {
return _repo.GetImages();
}
}
}
Since you did not specify an access modifier for your interface, it defaults to internal, which is lower than public. That means only code in the same assembly are aware of its existence.
A public class can implement an internal interface, because other code inside that same assembly would see your class with the interface, and code outside would simply see the public class, with no interface.
However, in File2, you are making an internal interface part of that class' public contract - that is, your class is public, which means any code can see it, but in order to use it they must also be able to understand the types in the constructor. Since one of the types required in the constructor is internal, external code cannot understand it, and this contract is impossible to fulfill.
What I don't understand is that MyClass can use the interface but ImageGetter class cannot.
ImageGetter can use the interface, it just can't present it in one of its method signatures which are more accessible. Likewise if MyClass tried to use it in one of its method signatures you'd get the same error. Read below.
Top level interfaces, structs, and classes default to internal access. To fix your problem put public before your interface declaration.
internal types in C# are accessible only within the same assembly.
You can't use a more restrictive access modifier in a less restrictive signature for obvious reasons. (How could someone who can't access the more restrictive type call the function for example?)
No, which file the code is in is irrelevant. The interface is private to the namespace, not the file.
The error message is not because the ImageGetter class can't reach the interface, it's because the class can't be used from outside the namespace.
The difference lies in how you use the interface. The class MyClass only implements the interface, so you can still use the class even if you can't use the interface. The constructor in the ImageGetter class requires a reference to the interface, so you can't use that class without also having access to the interface.
Interfaces and classes default to internal accessibility (because there is no concept of a class being private inside a namespace). The error explains what happens there - IImageRepository is less accessible (it is internal) than ImageGetter constructor, which exposes IImageRepository as a parameter.
Basically a public method on ImageGetter exposes a type that is internal. This is not allowed in C#.
All my college years I have been using public, and would like to know the difference between public, private, and protected?
Also what does static do as opposed to having nothing?
Access modifiers
From learn.microsoft.com:
public
The type or member can be accessed by any other code in the same assembly or another assembly that references it.
private
The type or member can only be accessed by code in the same class or struct.
protected
The type or member can only be accessed by code in the same class or struct, or in a derived class.
private protected (added in C# 7.2)
The type or member can only be accessed by code in the same class or struct, or in a derived class from the same assembly, but not from another assembly.
internal
The type or member can be accessed by any code in the same assembly, but not from another assembly.
protected internal
The type or member can be accessed by any code in the same assembly, or by any derived class in another assembly.
When no access modifier is set, a default access modifier is used. So there is always some form of access modifier even if it's not set.
static modifier
The static modifier on a class means that the class cannot be instantiated, and that all of its members are static. A static member has one version regardless of how many instances of its enclosing type are created.
A static class is basically the same as a non-static class, but there is one difference: a static class cannot be externally instantiated. In other words, you cannot use the new keyword to create a variable of the class type. Because there is no instance variable, you access the members of a static class by using the class name itself.
However, there is a such thing as a static constructor. Any class can have one of these, including static classes. They cannot be called directly & cannot have parameters (other than any type parameters on the class itself). A static constructor is called automatically to initialize the class before the first instance is created or any static members are referenced. Looks like this:
static class Foo()
{
static Foo()
{
Bar = "fubar";
}
public static string Bar { get; set; }
}
Static classes are often used as services, you can use them like so:
MyStaticClass.ServiceMethod(...);
A graphical overview (summary in a nutshell)
Actually, it's a little bit more complicated than that.
Now (as of C# 7.2), there's also private protected, and it matters whether a derived class is in the same assembly or not.
So the overview needs to be expanded:
See also the C#-dotnet-docs on the subject.
Since static classes are sealed, they cannot be inherited (except from Object), so the keyword protected is invalid on static classes.
For the defaults if you put no access modifier in front, see here:
Default visibility for C# classes and members (fields, methods, etc.)?
Non-nested
enum public
non-nested classes / structs internal
interfaces internal
delegates in namespace internal
class/struct member(s) private
delegates nested in class/struct private
Nested:
nested enum public
nested interface public
nested class private
nested struct private
Also, there is the sealed-keyword, which makes a class not-inheritable.
Also, in VB.NET, the keywords are sometimes different, so here a cheat-sheet:
Public - If you can see the class, then you can see the method
Private - If you are part of the class, then you can see the method, otherwise not.
Protected - Same as Private, plus all descendants can also see the method.
Static (class) - Remember the distinction between "Class" and "Object" ? Forget all that. They are the same with "static"... the class is the one-and-only instance of itself.
Static (method) - Whenever you use this method, it will have a frame of reference independent of the actual instance of the class it is part of.
Reposting the awesome diagrams from this answer.
Here are all access modifiers in Venn diagrams, from more limiting to more promiscuous:
private:
private protected: - added in C# 7.2
internal:
protected:
protected internal:
public:
Yet another visual approach of the current access modifier (C# 7.2). Hopefully the schema helps to remember it easier
(click the image for interactive view.)
Outside Inside
If you struggle to remember the two-worded access modifiers, remember outside-inside.
private protected: private outside (the same assembly) protected inside (same assembly)
protected internal: protected outside (the same assembly) internal inside (same assembly)
using System;
namespace ClassLibrary1
{
public class SameAssemblyBaseClass
{
public string publicVariable = "public";
protected string protectedVariable = "protected";
protected internal string protected_InternalVariable = "protected internal";
internal string internalVariable = "internal";
private string privateVariable = "private";
public void test()
{
// OK
Console.WriteLine(privateVariable);
// OK
Console.WriteLine(publicVariable);
// OK
Console.WriteLine(protectedVariable);
// OK
Console.WriteLine(internalVariable);
// OK
Console.WriteLine(protected_InternalVariable);
}
}
public class SameAssemblyDerivedClass : SameAssemblyBaseClass
{
public void test()
{
SameAssemblyDerivedClass p = new SameAssemblyDerivedClass();
// NOT OK
// Console.WriteLine(privateVariable);
// OK
Console.WriteLine(p.publicVariable);
// OK
Console.WriteLine(p.protectedVariable);
// OK
Console.WriteLine(p.internalVariable);
// OK
Console.WriteLine(p.protected_InternalVariable);
}
}
public class SameAssemblyDifferentClass
{
public SameAssemblyDifferentClass()
{
SameAssemblyBaseClass p = new SameAssemblyBaseClass();
// OK
Console.WriteLine(p.publicVariable);
// OK
Console.WriteLine(p.internalVariable);
// NOT OK
// Console.WriteLine(privateVariable);
// Error : 'ClassLibrary1.SameAssemblyBaseClass.protectedVariable' is inaccessible due to its protection level
//Console.WriteLine(p.protectedVariable);
// OK
Console.WriteLine(p.protected_InternalVariable);
}
}
}
using System;
using ClassLibrary1;
namespace ConsoleApplication4
{
class DifferentAssemblyClass
{
public DifferentAssemblyClass()
{
SameAssemblyBaseClass p = new SameAssemblyBaseClass();
// NOT OK
// Console.WriteLine(p.privateVariable);
// NOT OK
// Console.WriteLine(p.internalVariable);
// OK
Console.WriteLine(p.publicVariable);
// Error : 'ClassLibrary1.SameAssemblyBaseClass.protectedVariable' is inaccessible due to its protection level
// Console.WriteLine(p.protectedVariable);
// Error : 'ClassLibrary1.SameAssemblyBaseClass.protected_InternalVariable' is inaccessible due to its protection level
// Console.WriteLine(p.protected_InternalVariable);
}
}
class DifferentAssemblyDerivedClass : SameAssemblyBaseClass
{
static void Main(string[] args)
{
DifferentAssemblyDerivedClass p = new DifferentAssemblyDerivedClass();
// NOT OK
// Console.WriteLine(p.privateVariable);
// NOT OK
//Console.WriteLine(p.internalVariable);
// OK
Console.WriteLine(p.publicVariable);
// OK
Console.WriteLine(p.protectedVariable);
// OK
Console.WriteLine(p.protected_InternalVariable);
SameAssemblyDerivedClass dd = new SameAssemblyDerivedClass();
dd.test();
}
}
}
Regarding the question of Nothing
Namespace types are internal by default
Any type member, including nested types are private by default
public - can be access by anyone anywhere.
private - can only be accessed from with in the class it is a part of.
protected - can only be accessed from with in the class or any object that inherits off of the class.
Nothing is like null but in VB.
Static means you have one instance of that object, method for every instance of that class.
Hmm.
See here: Access Modifiers.
In a nutshell:
Public gives the method or type complete visibility from other types/classes.
Private allows only the type containing the private method/variable access to the private method/variable (note that nested classes also have access to the containing classes private methods/variables).
Protected is similar to private except derived classes can also access protected methods.
"Nothing" is VB.NET's equivalent to null. Although if you're referring to "nothing" meaning "no access modifier", then it depends, although a very rough rule of thumb (certainly in C#) is that if you don't explicitly specify an access modifier, the method/variable declaration is usually as restricted as it can be.
i.e.
public class MyClass
{
string s = "";
}
is effectively the same as:
public class MyClass
{
private string s = "";
}
The linked MSDN article will offer a fully description when there's no access modifier explicitly specified.
mmm...
Static means that you can access that function without having an instance of the class.
You can access directly from the class definition.
A status of Private indicates that variables can only be accessed by objects of the same class. Protected status extends that access to include descendants of the class as well.
"from the above table we can see the deference between private and protected... am think both are same ....so what the need for that two separate command"
Check MSDN link for more information
public means that it can be accessed by any class in any assembly, which includes the class itself.
protected internal means it can be accessed by the class itself (in the class definition), and it can be accessed by any class in the current assembly, but outside of the assembly it can only be accessed by a class that inherits the class, or by the class itself (if it is a partial class) – basically it means internal inside the assembly and protected outside of the assembly.
protected means it can only be accessed by the class itself, or accessed by a class that inherits it and that class can be in any assembly
internal means it can be accessed by the class itself or by any class in the assembly but can't be accessed at all outside of the assembly unless by the class itself (i.e. it is a partial class)
private protected means it can only be accessed by the class itself, or it can be accessed by a class that inherits it and only if that class is in the current assembly. Outside of the assembly, it can only be accessed by the class itself (i.e. it is a partial class) – basically combines internal and protected, or another way of putting it is it's private outside of the assembly and protected inside the assembly.
private means that it can only be accessed by the class itself
no access modifier: The default access for everything in C# is "the most restricted access you could declare for that member"., which is private for a member / method / nested class in a class and internal for a non-nested class.
In the above text 'accessed' means accessed via an object of the class type, which within a method of the class itself will be the implicit this object, or perhaps the method instantiates an explicit object of the current class type and accesses it through that object. Both are considered to be being accessed by the class itself and therefore the access rules are the same. This also applies to an access being performed from a static method, or when it is a static member / method being accessed, except the access is performed using the class scope and not and object. A member / method of a static class needs to be explicitly made static or it won't compile.
Classes that are not nested can either be public or internal and are internal by default. Classes that are nested can be any access type, and if the parent class is static, it does not need to be static and neither do its members. An internal class means that it can only be instantiated or its static members accessed from the current assembly.
You can have a public member / method / nested class in an internal or private nested class -- only the access specifier (in the fully qualified name of the access that is being made) that is below the level required in the current access that is being made prevents the access.
Inheritance in C# is always public unlike C++, which can privately or protectedly inherit, which then changes the access of all classes that then inherit from the class that is inheriting from this class, as well as the access via object / via class scope of type of the class that inheriting privately / protectedly from the class and the class that is inheriting from the class that is inheriting privately / protectedly form the class, and so on. The access is changed such that all access modifiers less restrictive than private or protected are made private and protected respectively.
Careful! Watch the accessibility of your classes. Public and protected classes and methods are by default accessible for everyone.
Also, Microsoft isn't very explicit in showing access modifiers (public, protected, etc.. keywords) when new classes in Visual Studio are created. So, take good care and think about the accessibility of your class because it's the door to your implementation internals.
Those access modifiers specify where your members are visible. You should probably read this up. Take the link given by IainMH as a starting point.
Static members are one per class and not one per instance.
I think it is related to good OOP design. If you are a developer of a library you want to hide the inner workings of your library. That way, you can modify your library inner workings later on. So you put your members and helper methods as private, and only interface methods are public. Methods that should be overwritten should be protected.
C# has, in total, 6 access modifiers:
private: The member declared with this accessibility can be visible within the containing type, it is not visible to any derived types, other types in the same assembly or types outside of the containing assembly. i.e., access is limited to the containing type only.
protected: The member declared with this accessibility can be visible within the types derived from the containing type within the containing assembly, and the types derived from the containing type outside of the containing assembly. i.e., access is limited to derived types of the containing type.
internal: The member declared with this accessibility can be visible within the assembly containing this member, it is not visible to any assembly outside of the containing assembly. i.e., access is limited to containing assembly only.
internal protected: The member declared with this accessibility can be visible within the types derived from the containing type within or outside of the containing assembly, it is also visible to any types within the containing assembly. i.e., access is limited to containing assembly or derived types.
public: The member declared with this accessibility can be visible within the assembly containing this member, or any other assembly that references the containing assembly. i.e., access is not limited.
In C# 7.2, a new level of accessibility was added:
private protected: The member declared with this accessibility can be visible within the types derived from this containing type within the containing assembly. It is not visible to any types not derived from the containing type, or outside of the containing assembly. i.e., the access is limited to derived types within the containing assembly.
Source including a sample code of the new private protected access modifier
public The type or member can be accessed by any other code in the same assembly or another assembly that references it. The accessibility level of public members of a type is controlled by the accessibility level of the type itself.
private The type or member can be accessed only by code in the same class or struct.
internal Internal types or members are accessible only within files in the same assembly.
protected The type or member can be accessed only by code in the same class, or in a class that is derived from that class.
internal: The type or member can be accessed by any code in the same assembly, but not from another assembly. In other words, internal types or members can be accessed from code that is part of the same compilation.
protected internal: The type or member can be accessed by any code in the assembly in which it's declared, or from within a derived class in another assembly.
private protected The type or member can be accessed by types derived from the class that are declared within its containing assembly.
protected internal A protected internal member is accessible from the current assembly or from types that are derived from the containing class.
static modifier is used to declare a static member, which belongs to the type itself rather than to a specific object. The static modifier can be used to declare static classes. In classes, interfaces, and structs, you may add the static modifier to fields, methods, properties, operators, events, and constructors.
With C# 11 you can also use the file access modifier.
The file modifier restricts a top-level type's scope and visibility to the file in which it's declared. The file modifier will generally be applied to types written by a source generator. File-local types provide source generators with a convenient way to avoid name collisions among generated types.
// In File1.cs:
file interface IWidget
{
int ProvideAnswer();
}
file class HiddenWidget
{
public int Work() => 42;
}
public class Widget : IWidget
{
public int ProvideAnswer()
{
var worker = new HiddenWidget();
return worker.Work();
}
}
I have created another type of visualization. Maybe this can be better way to understand for someone
https://github.com/TropinAlexey/C-sharp-Access-Modifiers
All access modifiers' descriptions for C#