In the following program, if both are "public" error signal is saying that Inconsistent accessibility.
Program is to executive correctly, if both accessibilities left unmentioned or one of the accessibility was mentioned "public". Problem is highlighted
Please explain the reason for differences.
using System;
public class Program
{
public static void Main()
{
IceCreamCone vanilla2 = new IceCreamCone("Vanilla", 2);
IceCreamCone chocolate1 = new IceCreamCone("Chocolate", 1);
FlavorScoop(vanilla2);
FlavorScoop(chocolate1);
Console.ReadKey();
}
public static void FlavorScoop(IceCreamCone icc)
{
Console.WriteLine(icc.GetFlavor() + " flavor" + ", " + icc.GetScoops() + " scoop");
}
}
class IceCreamCone
{
string flavor;
int scoop;
public IceCreamCone(string flavor, int scoop)
{
this.flavor = flavor;
this.scoop = scoop;
}
public string GetFlavor()
{
return flavor;
}
public int GetScoops()
{
return scoop;
}
}
Loading
Posted Aug 21, 2013, 9:21 AM
As this thread is too long it will continue in a different thread.
http://www.c-sharpcorner.com/Forums/Thread/226598/assembly.aspx
VulpesPosted Aug 21, 2013, 7:47 AM
However, code in these files can access types or members in any other file in the assembly provided that those types or members have internal or public access.
What I'm talking about here is not multi-file assemblies but assemblies which reference each other. Even the simplest assembly will reference (by default) an assembly called mscorlib.dll which contains the basic .NET framework types.
If you create a new console application in Visual Studio and click on References in solution explorer, you'll also see several other assemblies to which references are added by default and you can add your own references to these or remove some if you don't need them.
Normally, when you add references it will be to .dll assemblies but you can also add references to .exe assemblies, if you wish.
The point I've been trying to make in this thread is that the current assembly will have access to the public types and members in referenced assemblies but not to the internal types and members of those assemblies. The compiler therefore needs to take this into account when compiling the current assembly in case it is subsequently used as a referenced assembly itself.
Posted Aug 21, 2013, 6:41 AM
VulpesPosted Aug 20, 2013, 6:19 PM
Posted Aug 20, 2013, 3:39 PM
Under this circumstance what is meant by "outside". Problem is coloured below.
In the above 3rd thread 4th paragraph sentence is beginning with "This is because code outside the current assembly could………………….."
VulpesPosted Aug 20, 2013, 10:43 AM
After this (single module) assembly has been compiled, it could (in theory) be added as a reference to another assembly. If code in the latter assembly wanted to call the FlavorScoop method, it would have problems in doing so because it wouldn't have access to the IceCreamCone class.
The compiler is therefore ensuring that these problems won't arise by flagging it as an error now.
Posted Aug 20, 2013, 10:32 AM
indicates Multifile Assembly.
VulpesPosted Aug 20, 2013, 6:11 AM
Assemblies can either be executables (.exes) or class libraries (.dlls).
The program in your opening post would produce an executable assembly and, whilst that program was running, it would be the current assembly.
The internal access modifier means that only code within the current assembly can access the type or member concerned.
The public access modifier means that any code (whether in the current assembly or not) can access the type or member.
Clearly, if code can access a method then it needs to also have access to the types of the parameters which that method uses. Otherwise, it's not going to be able to call the method.
So, code outside the current assembly attempting to call the public FlavorScoop method also needs access to the IceCreamCone class which it doesn't have because this class is internal. The C# compiler therefore flags it as an 'inconsistent accessibility' error.
Posted Aug 20, 2013, 5:07 AM
Which is the current assembly?
Posted Aug 19, 2013, 10:46 AM
VulpesPosted Aug 19, 2013, 10:26 AM
I think that most developers would agree with those sentiments and, as there isn't a good reason for adding an access modifier to the Main() method, it's best left alone.
Posted Aug 19, 2013, 10:15 AM
How is it in practice, when working in programming filed?
VulpesPosted Aug 19, 2013, 9:58 AM
However, it doesn't make any difference what access modifier is used with the static Main method. As this is the entry-point to the application, the CLR can find it even if its private (the default) and consequently this is what you generally see in VS projects and in most other professionally written code.
Posted Aug 19, 2013, 9:40 AM
VulpesPosted Aug 18, 2013, 7:02 PM
Posted Aug 18, 2013, 6:53 PM
VulpesPosted Aug 18, 2013, 4:21 PM
This is no problem whether the Program class is public or (by default) internal because the FlavorScoop method can only be accessed from within the Program class itself and code within that class has access to the internal class IceCreamCone.
However, if the Program class is public and you make the FlavorScoop method public as well, then this does prevent an access problem.
This is because code outside the current assembly could (in theory at least) call the FlavorScoop method but would be thwarted from doing so because it wouldn't have access to the IceCreamCone class which is internal to the current assembly. External code would not therefore be able to pass the FlavorScoop an appropriate parameter and so the compiler won't allow it in these circumstances.
However, if the FlavorScoop method were public but the Program class were internal, this wouldn't be a problem because the method couldn't then be called by external code. The accessibility of members of a class is always tempered by the accessibility of the class itself and the FlavorScoop method would be effectively internal in this situation.
VulpesPosted Aug 18, 2013, 1:58 PM
The FlavorScoop method has a parameter of this type and since it's a public method of a public class this is regarded by the compiler as inconsistent accessibility.
To fix it, make IceCreamCone a public class.