Last active
September 30, 2020 17:17
-
-
Save manoj-choudhari-git/069f0be532d2b30b276a77669127d3c8 to your computer and use it in GitHub Desktop.
Usage of Protected Internal 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); | |
} | |
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