Solved

Clarification on 'Internal' access modifier in c#

Posted on 2010-09-23
7
373 Views
Last Modified: 2012-06-27
I have been revising for an upcoming test on .net/c# and would like to clarify something. I like to fully understand things before I 'tick' them off in my head.

So, within .net, we have the following access modifiers;

Public (no problem here)
Private (no problem here)
Protected (no problem)

However, I am a little confused with the 'internal' and then subsequent 'protected internal'

The MSDN explanation of 'internal' is "Access is limited to the current assembly."

Now,my understanding of an assembly is a collection of modules and resources compiled into an 'assembly' be that .exe or .dll

So based on this assumption, how can an internal modifier be limited to an assembly, based on this;

I have an 'assembly' in which i have various files, which contain my custom classes. If I declared an 'internal' value (int) in one of these classes, it will compile. If I then instantiated this class from another assembly (my ui), I can access it.

Now I do see that when you create an 'internal' class, then it is inaccessible from another assembly. So, what about the internal value (int). Am I basically breaking an unwritten rule by declaring my value (int) as internal or am I missing something completely.

Although not tested, I would assume the same is going to come up with protected internal to some degree.

Can anyone clarify this for me.



0
Comment
Question by:officedog
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 30

Expert Comment

by:anarki_jimbel
ID: 33749967
OK, from my experience.
I use internal modifier mainly for some methods is classes. This means I can access these methods only in the current project (which will be compiled into an assembly). The main reasoning to have these methods only inside the project E.g., I have a project for all my user controls. And, e.g., I have some specific MyTextBox that is used by some other controls, e.g. by MyComboBox. Outside the project I want to use both MyTextBox and MyComboBox. But some methods of the MyTextBox should be used by MyCombo, therefore I mark them as "internal. So, they look like "public" inside the project but not visible outside.
0
 
LVL 30

Expert Comment

by:anarki_jimbel
ID: 33750065
Protected internal is used to deny access to parts of a class' functionality to any descendant classes found in other applications. In other words, if I inherit a class with a protected internal method in another project - I should not see it.

0
 
LVL 9

Expert Comment

by:shadow77
ID: 33750432
Private is restrictive; it only allows access by members of the class in which the variable or method is declared.

Public is non-restrictive; it allows access from anywhere.

Internal and Protected fall in between.  Internal is like "public, but only withing this assembly", while protected is like "public but only for the declaring class and its sub-classes"

Protected Internal means Protected or Internal, that is "public within this assembly or within this class and its sub-classes".
0
Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

 
LVL 26

Accepted Solution

by:
Anurag Thakur earned 125 total points
ID: 33750534
0
 
LVL 30

Assisted Solution

by:anarki_jimbel
anarki_jimbel earned 125 total points
ID: 33750848
The first link from ragi0017 is a good one. I'll try to explain it in my interpretation. I just did some testing and found thew following.

1. If a method is marked as "protected internal" - this means that for the current project (assembly) it's both internal, i.e. can be visible anyway in the current project. And it can be inherited as any protected method. In other words, does not make big difference in the same project: it could be marked as public or just internal.

2. For another project we haver some difference. If we create a class MyTestClass2  inheriting from the first project's class MyTestClass, protected method will be inherited and will be visible inside the new class. I.e., "internal" does not work for inheritance.
However, if we try to create an instance of the MyTestClass  in the second project:

TestProj1.MyTestClass c = new TestProj1.MyTestClass();

the method won't be visible for the "c" object, because the method is "internal" for the first project.

Hope it's not too confusing.
0
 

Author Comment

by:officedog
ID: 33751826
Great got it all now thanks. I think the biggest help was this statement from the page that ragi0017 linked.

'protected internal' does not mean protected *and* internal - it means protected *or* internal.  

Thanks all :)

0
 

Author Closing Comment

by:officedog
ID: 33751829
Closed. Points awarded to ragi0017 and anarki_jimbel.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
insert value of checklistbox checked 4 39
defining NULL or 0 10 51
How to force output to ascii 2 40
parsing question on dynamic concatenation 3 34
Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question