Skip to content

Instantly share code, notes, and snippets.

@manoj-choudhari-git
Last active September 30, 2020 17:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save manoj-choudhari-git/069f0be532d2b30b276a77669127d3c8 to your computer and use it in GitHub Desktop.
Save manoj-choudhari-git/069f0be532d2b30b276a77669127d3c8 to your computer and use it in GitHub Desktop.
Usage of Protected Internal in C#
/* =========================================================================
* First Assembly (ProjectOne.csproj)
* =========================================================================*/
namespace ProjectOne
{
public class Sample
{
public void SomeMethod()
{
// Allowed from same class
var result = Calculate(3, 5);
}
protected internal int Calculate(int x, int y)
{
return x + y;
}
}
public class DerivedSample : Sample
{
public void SomeOtherMethod()
{
// Derived class, So allowed
var result = Calculate(4, 6);
}
}
public class AnotherSample
{
public void SomeOtherMethod()
{
Sample s = new Sample();
// Same assembly, so allowed
var result = s.Calculate(4, 6);
}
}
}
/* =========================================================================
* Second Assembly (ProjectTwo.csproj)
* =========================================================================*/
namespace ProjectTwo
{
using ProjectOne;
public class DerivedSampleTwo: Sample
{
public void SomeOtherMethod()
{
// Derived class not in declaring assembly
// So, access is allowed
var result = Calculate(9, 8);
}
}
public class RandomClass
{
public void SomeOtherMethod()
{
Sample s = new Sample();
// Random class, not in declaring assembly
// So below line is not valid
// s.Calculate(10, 10);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment