Why inheriting from a Collection changes accessibility levels?










-2














I've created 3 classes, A, B and C.



  • B inherits from A

  • C inherits a Collection of A

However, I can't understand why when inheriting from a collection, the derived class can access only the protected member of the base class.



public class A

public int MyPublic get; set;
protected int MyProtected get; set;
internal int MyInternal get; set;
protected internal int MyProtectedInternal get; set;

public class B : A

B MyB get; set;
B()

MyB.MyProtected++;
MyB.MyInternal++;
MyB.MyPublic++;
MyB.MyProtectedInternal++;


public class C : Collection<A>

C MyC get; set;
C()

MyC[0].MyProtected++; // Inaccessible due to its protection level.
MyC[0].MyInternal++;
MyC[0].MyPublic++;
MyC[0].MyProtectedInternal++;











share|improve this question



















  • 6




    C does not inherit from A. C inherits from Collection<T>.
    – BJ Myers
    Nov 11 '18 at 21:20











  • Is there a way to access a a protected member when I want to use this kind of architecture, when i need a subclass that is-a collection of a base class?
    – eminem
    Nov 11 '18 at 21:24











  • @eminem No. Protected members are visible only within the declaring type and within derived types. You're looking for something similar to friend from C++, which does not exist in C#. The closest similar modifier is internal and then both types must be in the same assembly. However, beware of coupling your classes tightly. Make the property internal, public, or rethink your architecture.
    – cdhowie
    Nov 11 '18 at 21:25







  • 3




    @eminem C is not a derived class of A. This sounds like an XY problem. Tell us what you're trying to accomplish by doing this.
    – cdhowie
    Nov 11 '18 at 21:28







  • 1




    You should not inherit from collections in the first place.
    – Adrian
    Nov 11 '18 at 21:38















-2














I've created 3 classes, A, B and C.



  • B inherits from A

  • C inherits a Collection of A

However, I can't understand why when inheriting from a collection, the derived class can access only the protected member of the base class.



public class A

public int MyPublic get; set;
protected int MyProtected get; set;
internal int MyInternal get; set;
protected internal int MyProtectedInternal get; set;

public class B : A

B MyB get; set;
B()

MyB.MyProtected++;
MyB.MyInternal++;
MyB.MyPublic++;
MyB.MyProtectedInternal++;


public class C : Collection<A>

C MyC get; set;
C()

MyC[0].MyProtected++; // Inaccessible due to its protection level.
MyC[0].MyInternal++;
MyC[0].MyPublic++;
MyC[0].MyProtectedInternal++;











share|improve this question



















  • 6




    C does not inherit from A. C inherits from Collection<T>.
    – BJ Myers
    Nov 11 '18 at 21:20











  • Is there a way to access a a protected member when I want to use this kind of architecture, when i need a subclass that is-a collection of a base class?
    – eminem
    Nov 11 '18 at 21:24











  • @eminem No. Protected members are visible only within the declaring type and within derived types. You're looking for something similar to friend from C++, which does not exist in C#. The closest similar modifier is internal and then both types must be in the same assembly. However, beware of coupling your classes tightly. Make the property internal, public, or rethink your architecture.
    – cdhowie
    Nov 11 '18 at 21:25







  • 3




    @eminem C is not a derived class of A. This sounds like an XY problem. Tell us what you're trying to accomplish by doing this.
    – cdhowie
    Nov 11 '18 at 21:28







  • 1




    You should not inherit from collections in the first place.
    – Adrian
    Nov 11 '18 at 21:38













-2












-2








-2







I've created 3 classes, A, B and C.



  • B inherits from A

  • C inherits a Collection of A

However, I can't understand why when inheriting from a collection, the derived class can access only the protected member of the base class.



public class A

public int MyPublic get; set;
protected int MyProtected get; set;
internal int MyInternal get; set;
protected internal int MyProtectedInternal get; set;

public class B : A

B MyB get; set;
B()

MyB.MyProtected++;
MyB.MyInternal++;
MyB.MyPublic++;
MyB.MyProtectedInternal++;


public class C : Collection<A>

C MyC get; set;
C()

MyC[0].MyProtected++; // Inaccessible due to its protection level.
MyC[0].MyInternal++;
MyC[0].MyPublic++;
MyC[0].MyProtectedInternal++;











share|improve this question















I've created 3 classes, A, B and C.



  • B inherits from A

  • C inherits a Collection of A

However, I can't understand why when inheriting from a collection, the derived class can access only the protected member of the base class.



public class A

public int MyPublic get; set;
protected int MyProtected get; set;
internal int MyInternal get; set;
protected internal int MyProtectedInternal get; set;

public class B : A

B MyB get; set;
B()

MyB.MyProtected++;
MyB.MyInternal++;
MyB.MyPublic++;
MyB.MyProtectedInternal++;


public class C : Collection<A>

C MyC get; set;
C()

MyC[0].MyProtected++; // Inaccessible due to its protection level.
MyC[0].MyInternal++;
MyC[0].MyPublic++;
MyC[0].MyProtectedInternal++;








c# inheritance






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 '18 at 0:51









Charlie

5932826




5932826










asked Nov 11 '18 at 21:18









eminem

31




31







  • 6




    C does not inherit from A. C inherits from Collection<T>.
    – BJ Myers
    Nov 11 '18 at 21:20











  • Is there a way to access a a protected member when I want to use this kind of architecture, when i need a subclass that is-a collection of a base class?
    – eminem
    Nov 11 '18 at 21:24











  • @eminem No. Protected members are visible only within the declaring type and within derived types. You're looking for something similar to friend from C++, which does not exist in C#. The closest similar modifier is internal and then both types must be in the same assembly. However, beware of coupling your classes tightly. Make the property internal, public, or rethink your architecture.
    – cdhowie
    Nov 11 '18 at 21:25







  • 3




    @eminem C is not a derived class of A. This sounds like an XY problem. Tell us what you're trying to accomplish by doing this.
    – cdhowie
    Nov 11 '18 at 21:28







  • 1




    You should not inherit from collections in the first place.
    – Adrian
    Nov 11 '18 at 21:38












  • 6




    C does not inherit from A. C inherits from Collection<T>.
    – BJ Myers
    Nov 11 '18 at 21:20











  • Is there a way to access a a protected member when I want to use this kind of architecture, when i need a subclass that is-a collection of a base class?
    – eminem
    Nov 11 '18 at 21:24











  • @eminem No. Protected members are visible only within the declaring type and within derived types. You're looking for something similar to friend from C++, which does not exist in C#. The closest similar modifier is internal and then both types must be in the same assembly. However, beware of coupling your classes tightly. Make the property internal, public, or rethink your architecture.
    – cdhowie
    Nov 11 '18 at 21:25







  • 3




    @eminem C is not a derived class of A. This sounds like an XY problem. Tell us what you're trying to accomplish by doing this.
    – cdhowie
    Nov 11 '18 at 21:28







  • 1




    You should not inherit from collections in the first place.
    – Adrian
    Nov 11 '18 at 21:38







6




6




C does not inherit from A. C inherits from Collection<T>.
– BJ Myers
Nov 11 '18 at 21:20





C does not inherit from A. C inherits from Collection<T>.
– BJ Myers
Nov 11 '18 at 21:20













Is there a way to access a a protected member when I want to use this kind of architecture, when i need a subclass that is-a collection of a base class?
– eminem
Nov 11 '18 at 21:24





Is there a way to access a a protected member when I want to use this kind of architecture, when i need a subclass that is-a collection of a base class?
– eminem
Nov 11 '18 at 21:24













@eminem No. Protected members are visible only within the declaring type and within derived types. You're looking for something similar to friend from C++, which does not exist in C#. The closest similar modifier is internal and then both types must be in the same assembly. However, beware of coupling your classes tightly. Make the property internal, public, or rethink your architecture.
– cdhowie
Nov 11 '18 at 21:25





@eminem No. Protected members are visible only within the declaring type and within derived types. You're looking for something similar to friend from C++, which does not exist in C#. The closest similar modifier is internal and then both types must be in the same assembly. However, beware of coupling your classes tightly. Make the property internal, public, or rethink your architecture.
– cdhowie
Nov 11 '18 at 21:25





3




3




@eminem C is not a derived class of A. This sounds like an XY problem. Tell us what you're trying to accomplish by doing this.
– cdhowie
Nov 11 '18 at 21:28





@eminem C is not a derived class of A. This sounds like an XY problem. Tell us what you're trying to accomplish by doing this.
– cdhowie
Nov 11 '18 at 21:28





1




1




You should not inherit from collections in the first place.
– Adrian
Nov 11 '18 at 21:38




You should not inherit from collections in the first place.
– Adrian
Nov 11 '18 at 21:38












2 Answers
2






active

oldest

votes


















0














Firstly, and probably not strictly relevant to your question, I'm not sure why C has a member MyC. Type C is a Collection<A> so you can just use the indexer this[int x] to access the collection items.



To give C access to ALL of the members of A while still limiting access for anything else, you could make C a nested type.



public class A

protected int MyProtected get; set;
private int MyPrivate get; set;

public class C : Collection<A>

public void Foo()

this[0].MyProtected++; // Allowed
this[0].MyPrivate++; // Also allowed!





Note that this gives C more access to A than you intended (all member regardless of access modifier).



Some discussion of pros/cons of nested classes here: What are reasons why one would want to use nested classes?






share|improve this answer




















  • This seems like a possible solution :) Thank you.
    – eminem
    Nov 12 '18 at 8:00


















0














As mentioned in the comments, you aren't actually inheriting from A. You're inheriting from Collection<T>.



If your goal is to expose the protected properties of A, then make a class that inherits from A and make public properties of the same name that just modify the properties of the inherited class. Then make a collection of that new class.



Something like this. Notice the new class D:



public class D : A 
new public int MyProtected
get return base.MyProtected;
set base.MyProtected = value;

new public int MyProtectedInternal
get return base.MyProtectedInternal;
set base.MyProtectedInternal = value;


public class C : Collection<D>

C MyC get; set;
C()

MyC[0].MyProtected++;
MyC[0].MyInternal++;
MyC[0].MyPublic++;
MyC[0].MyProtectedInternal++;




The new modifier keyword indicates that you are replacing the property from the inherited type.






share|improve this answer




















  • Wouldnt that expose MyProtected (for it being public now) to classes that are not derived from D? Remember I need MyProtected to be accessible only to derived classes.
    – eminem
    Nov 11 '18 at 21:44






  • 2




    But you also seem to want it to be accessed in a Collection, which means it can't be protected. You will need to choose which is more important to you.
    – Gabriel Luci
    Nov 11 '18 at 21:47










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53253333%2fwhy-inheriting-from-a-collectiont-changes-accessibility-levels%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














Firstly, and probably not strictly relevant to your question, I'm not sure why C has a member MyC. Type C is a Collection<A> so you can just use the indexer this[int x] to access the collection items.



To give C access to ALL of the members of A while still limiting access for anything else, you could make C a nested type.



public class A

protected int MyProtected get; set;
private int MyPrivate get; set;

public class C : Collection<A>

public void Foo()

this[0].MyProtected++; // Allowed
this[0].MyPrivate++; // Also allowed!





Note that this gives C more access to A than you intended (all member regardless of access modifier).



Some discussion of pros/cons of nested classes here: What are reasons why one would want to use nested classes?






share|improve this answer




















  • This seems like a possible solution :) Thank you.
    – eminem
    Nov 12 '18 at 8:00















0














Firstly, and probably not strictly relevant to your question, I'm not sure why C has a member MyC. Type C is a Collection<A> so you can just use the indexer this[int x] to access the collection items.



To give C access to ALL of the members of A while still limiting access for anything else, you could make C a nested type.



public class A

protected int MyProtected get; set;
private int MyPrivate get; set;

public class C : Collection<A>

public void Foo()

this[0].MyProtected++; // Allowed
this[0].MyPrivate++; // Also allowed!





Note that this gives C more access to A than you intended (all member regardless of access modifier).



Some discussion of pros/cons of nested classes here: What are reasons why one would want to use nested classes?






share|improve this answer




















  • This seems like a possible solution :) Thank you.
    – eminem
    Nov 12 '18 at 8:00













0












0








0






Firstly, and probably not strictly relevant to your question, I'm not sure why C has a member MyC. Type C is a Collection<A> so you can just use the indexer this[int x] to access the collection items.



To give C access to ALL of the members of A while still limiting access for anything else, you could make C a nested type.



public class A

protected int MyProtected get; set;
private int MyPrivate get; set;

public class C : Collection<A>

public void Foo()

this[0].MyProtected++; // Allowed
this[0].MyPrivate++; // Also allowed!





Note that this gives C more access to A than you intended (all member regardless of access modifier).



Some discussion of pros/cons of nested classes here: What are reasons why one would want to use nested classes?






share|improve this answer












Firstly, and probably not strictly relevant to your question, I'm not sure why C has a member MyC. Type C is a Collection<A> so you can just use the indexer this[int x] to access the collection items.



To give C access to ALL of the members of A while still limiting access for anything else, you could make C a nested type.



public class A

protected int MyProtected get; set;
private int MyPrivate get; set;

public class C : Collection<A>

public void Foo()

this[0].MyProtected++; // Allowed
this[0].MyPrivate++; // Also allowed!





Note that this gives C more access to A than you intended (all member regardless of access modifier).



Some discussion of pros/cons of nested classes here: What are reasons why one would want to use nested classes?







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 12 '18 at 1:58









andrew

840518




840518











  • This seems like a possible solution :) Thank you.
    – eminem
    Nov 12 '18 at 8:00
















  • This seems like a possible solution :) Thank you.
    – eminem
    Nov 12 '18 at 8:00















This seems like a possible solution :) Thank you.
– eminem
Nov 12 '18 at 8:00




This seems like a possible solution :) Thank you.
– eminem
Nov 12 '18 at 8:00













0














As mentioned in the comments, you aren't actually inheriting from A. You're inheriting from Collection<T>.



If your goal is to expose the protected properties of A, then make a class that inherits from A and make public properties of the same name that just modify the properties of the inherited class. Then make a collection of that new class.



Something like this. Notice the new class D:



public class D : A 
new public int MyProtected
get return base.MyProtected;
set base.MyProtected = value;

new public int MyProtectedInternal
get return base.MyProtectedInternal;
set base.MyProtectedInternal = value;


public class C : Collection<D>

C MyC get; set;
C()

MyC[0].MyProtected++;
MyC[0].MyInternal++;
MyC[0].MyPublic++;
MyC[0].MyProtectedInternal++;




The new modifier keyword indicates that you are replacing the property from the inherited type.






share|improve this answer




















  • Wouldnt that expose MyProtected (for it being public now) to classes that are not derived from D? Remember I need MyProtected to be accessible only to derived classes.
    – eminem
    Nov 11 '18 at 21:44






  • 2




    But you also seem to want it to be accessed in a Collection, which means it can't be protected. You will need to choose which is more important to you.
    – Gabriel Luci
    Nov 11 '18 at 21:47















0














As mentioned in the comments, you aren't actually inheriting from A. You're inheriting from Collection<T>.



If your goal is to expose the protected properties of A, then make a class that inherits from A and make public properties of the same name that just modify the properties of the inherited class. Then make a collection of that new class.



Something like this. Notice the new class D:



public class D : A 
new public int MyProtected
get return base.MyProtected;
set base.MyProtected = value;

new public int MyProtectedInternal
get return base.MyProtectedInternal;
set base.MyProtectedInternal = value;


public class C : Collection<D>

C MyC get; set;
C()

MyC[0].MyProtected++;
MyC[0].MyInternal++;
MyC[0].MyPublic++;
MyC[0].MyProtectedInternal++;




The new modifier keyword indicates that you are replacing the property from the inherited type.






share|improve this answer




















  • Wouldnt that expose MyProtected (for it being public now) to classes that are not derived from D? Remember I need MyProtected to be accessible only to derived classes.
    – eminem
    Nov 11 '18 at 21:44






  • 2




    But you also seem to want it to be accessed in a Collection, which means it can't be protected. You will need to choose which is more important to you.
    – Gabriel Luci
    Nov 11 '18 at 21:47













0












0








0






As mentioned in the comments, you aren't actually inheriting from A. You're inheriting from Collection<T>.



If your goal is to expose the protected properties of A, then make a class that inherits from A and make public properties of the same name that just modify the properties of the inherited class. Then make a collection of that new class.



Something like this. Notice the new class D:



public class D : A 
new public int MyProtected
get return base.MyProtected;
set base.MyProtected = value;

new public int MyProtectedInternal
get return base.MyProtectedInternal;
set base.MyProtectedInternal = value;


public class C : Collection<D>

C MyC get; set;
C()

MyC[0].MyProtected++;
MyC[0].MyInternal++;
MyC[0].MyPublic++;
MyC[0].MyProtectedInternal++;




The new modifier keyword indicates that you are replacing the property from the inherited type.






share|improve this answer












As mentioned in the comments, you aren't actually inheriting from A. You're inheriting from Collection<T>.



If your goal is to expose the protected properties of A, then make a class that inherits from A and make public properties of the same name that just modify the properties of the inherited class. Then make a collection of that new class.



Something like this. Notice the new class D:



public class D : A 
new public int MyProtected
get return base.MyProtected;
set base.MyProtected = value;

new public int MyProtectedInternal
get return base.MyProtectedInternal;
set base.MyProtectedInternal = value;


public class C : Collection<D>

C MyC get; set;
C()

MyC[0].MyProtected++;
MyC[0].MyInternal++;
MyC[0].MyPublic++;
MyC[0].MyProtectedInternal++;




The new modifier keyword indicates that you are replacing the property from the inherited type.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 11 '18 at 21:37









Gabriel Luci

10.4k11324




10.4k11324











  • Wouldnt that expose MyProtected (for it being public now) to classes that are not derived from D? Remember I need MyProtected to be accessible only to derived classes.
    – eminem
    Nov 11 '18 at 21:44






  • 2




    But you also seem to want it to be accessed in a Collection, which means it can't be protected. You will need to choose which is more important to you.
    – Gabriel Luci
    Nov 11 '18 at 21:47
















  • Wouldnt that expose MyProtected (for it being public now) to classes that are not derived from D? Remember I need MyProtected to be accessible only to derived classes.
    – eminem
    Nov 11 '18 at 21:44






  • 2




    But you also seem to want it to be accessed in a Collection, which means it can't be protected. You will need to choose which is more important to you.
    – Gabriel Luci
    Nov 11 '18 at 21:47















Wouldnt that expose MyProtected (for it being public now) to classes that are not derived from D? Remember I need MyProtected to be accessible only to derived classes.
– eminem
Nov 11 '18 at 21:44




Wouldnt that expose MyProtected (for it being public now) to classes that are not derived from D? Remember I need MyProtected to be accessible only to derived classes.
– eminem
Nov 11 '18 at 21:44




2




2




But you also seem to want it to be accessed in a Collection, which means it can't be protected. You will need to choose which is more important to you.
– Gabriel Luci
Nov 11 '18 at 21:47




But you also seem to want it to be accessed in a Collection, which means it can't be protected. You will need to choose which is more important to you.
– Gabriel Luci
Nov 11 '18 at 21:47

















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53253333%2fwhy-inheriting-from-a-collectiont-changes-accessibility-levels%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

Syphilis

Darth Vader #20