Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post Dec 25, 2012 06:03 AM by amigo 1
Dec 21, 2012 05:39 AM|LINK
// Compile with: /target:library
internal class BaseClass
public static int intM = 0;
// Compile with: /reference:Assembly1.dll
static void Main()
BaseClass myBase = new BaseClass(); // CS0122
This piece of code is taken from msdn and is an incorrect piece of coding . The point is to demonstrate internal keyword.
Anyway how to link these 2 assemblies ---is my question here?
Dec 21, 2012 05:53 AM|LINK
Yes you got confused with the internal keyword. If you want to link the assembly (Two different) use the public. So that assembly one exposed class can be accessed by assembly 2.
Internal means: Internal to assembly. That means your class BaseClass can be acccessed with-in the assembly (Assembly1.dll) not outside of the binary dll in which it is defined. Hope this helps. Do further study on the same topic.
Dec 21, 2012 08:01 AM|LINK
and is an incorrect piece of coding
It is not an incorrect piece of coding.
What the sample shows is that if you were to compile the code for the BaseClass into an assembly named Assembly1, and then compile the TestClass into a different assembly (Assembly1_a), which references Assembly1, the line of code marked with the comment
// CS0122 would, indeed, generate the compiler error CS0122 because BaseClass is not visible to code outside of Assembly1.
Note that unless you state otherwise - by applying the public keyword - all top level classes are internal by default. This ensures that developers are forced to "opt in" to making their types accessible from other assemblies. This is closely analagous to
members in classes being private by default, and is a solid example of choosing good, secure, defaults.
As to your question, if you were building the code and wanted to expose BaseClass outside of its assembly, simply change the access modifier to public.
Dec 25, 2012 06:03 AM|LINK
Thank you Dave
You have really helped me many a times