What is Protected Internal Access Modifier
in C# (C Sharp)?

If you want a member to be accessible only by the class containing it or its derived class which belongs to the same assembly in which the class is put up, then you have to use protected internal access modifier.




To demonstrate it, follow the steps shown below:

• Create the following class:
public class sampleClass {
protected internal int member1;
}

• Name the class as sampleAssembly.cs and Compile this class by specifying /target:library

• Create a new program called testClass.cs with the following code:
public class testClass:sampleClass {
public static void Main() {
member1 = 10;
}
}

• Compile this program by specifying /reference:sampleAssembly.dll

• Now try to execute testClass. You will end up in the following error:

“‘sampleClass.member1’ not visible outside assembly”

This is because, you try to access protected internal member of sampleClass from its derived class which is referencing the assembly but not belonging to that assembly. If the testClass is also part of sampleAssembly then you can very well access member1 without any errors.

Note that protected internal access modifier should not be used as a modifier on a class. If you try to use it, then you will end up in the following error: “Namespace elements cannot be explicitly declared as private, protected, or protected internal”

_______________________________________________________________________

Add to My Yahoo!


FREE Subscription

Subscribe to our mailing list and receive new articles
through email. Keep yourself updated with latest
developments in the industry.

Name:
Email:

Note : We never rent, trade, or sell my email lists to
anyone. We assure that your privacy is respected
and protected.

Visit .NET Programming Tutorial Homepage

______________________________________________________

Recommended Resource

| What is Private Access Modifier in C#? | What is Protected Access Modifier in C#? | What is Protected Internal Access Modifier in C#? | What is Public Access Modifier in C#? | What is the difference between virtual and abstract keywords in .NET? | What is the importance of Microsoft Application Blocks in .NET Architecture? | What is the need for Factory Method in C# | What is the purpose of ArrayList in .NET? | What is the purpose of Datareader in ADO.NET? | What is the purpose of Dataset in ADO.NET? | What is the purpose of finally block in C#? | What is the purpose of interlocked class in .NET? | What is the purpose of main() function in C# | What is the purpose of ManualResetEvent in .NET? | What is the purpose of sealed method in C#? | What is the purpose of Thread.Join() method in .NET? | What is the purpose of Thread.Sleep() method in .NET? | What is the purpose of throw keyword in C#? | What is the usage of ENUM in .NET? |


| Privacy Policy for www.dotnet-guide.com | Disclosure | Contact |

Copyright - © 2004 - 2017 - All Rights Reserved.