Skip to content

Instantly share code, notes, and snippets.

@manoj-choudhari-git
Created September 30, 2020 17:18
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/7fa5327b6edfb96ddcd7737deb0a38a9 to your computer and use it in GitHub Desktop.
Save manoj-choudhari-git/7fa5327b6edfb96ddcd7737deb0a38a9 to your computer and use it in GitHub Desktop.
Usage of Private Protected in C#
/* =========================================================================
* First Assembly (ProjectOne.csproj)
* =========================================================================*/
namespace ProjectOne
{
public class Sample
{
public void SomeMethod()
{
// Allowed from same class
var result = Calculate(3, 5);
}
private protected int Calculate(int x, int y)
{
return x + y;
}
}
public class DerivedSample : Sample
{
public void SomeOtherMethod()
{
// Allowed, as Derived class is in same assembly
var result = Calculate(4, 6);
}
}
public class AnotherSample
{
public void SomeOtherMethod()
{
Sample s = new Sample();
// Not Allowed, even if declaring class is in same assembly
// var result = s.Calculate(4, 6);
}
}
}
/* =========================================================================
* Second Assembly (ProjectTwo.csproj)
* =========================================================================*/
namespace ProjectTwo
{
using ProjectOne;
public class DerivedSampleTwo: Sample
{
public void SomeOtherMethod()
{
// Not Allowed, if derived class is in any other assembly
// var result = Calculate(9, 8);
}
}
public class RandomClass
{
public void SomeOtherMethod()
{
Sample s = new Sample();
// Not Allowed, from any other assembly
s.Calculate(10, 10);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment