Created
September 30, 2020 17:18
-
-
Save manoj-choudhari-git/7fa5327b6edfb96ddcd7737deb0a38a9 to your computer and use it in GitHub Desktop.
Usage of Private Protected in C#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* ========================================================================= | |
* 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