What is Protected Internal Access Modifier in C#?

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”

| 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? |


“Amazon and the Amazon logo are trademarks of Amazon.com, Inc. or its affiliates.”

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

Copyright - © 2004 - 2024 - All Rights Reserved.