-
The 'internal' keyword.
Hi All,
A nagging doubt.
Why is there a need for access modifier named 'internal' in C# which is partially
borrowed from Java keyword "Protected"(Java is kind of confusing clubbing
access to derived classes and package classes!).
Apart from the literal meaning , that it can be accessed by types in the
same assembly or Package(Java) , can somebody suggest a situation where the
addition of this keyword to C# language has justification.
Me coming from C++ style of coding I can think this keyword as near to 'friend
' keyword. But the keyword friend is used specifically by the class implementor/designer
and has control over which classes to allow access. While in case of 'internal'
the control shifts to the assembly/package designer as which classes has
access depends on the classes inside the assembly/package. Why did the drafters
of Java and C# language specification think of this as a nice idea?
I can also give a parallel in 'C'. The keyword 'static' which restricts the
scope of the variable/method to the file in which the method or variable
is declared. Were they thinking about this when they thought of the keyword
'internal'.
Is there anything similar to friend keyword in C# where I can limit access
to only a certain set of classes and not all classes in the assembly.
Please do not confuse with friend keyword in VB.NET. That is similar to the
keyword 'internal'.
Thanks in advance
Sridhar Mahadevan
-
Re: The 'internal' keyword.
One addition I want to make to my question.
I understand the need for the internal keyword at the type level. This is
similar to the 'Static' keyword of C limiting the Visibility of types inside
a assembly/package(mostly designed to serve some common purpose- graphics
package). This is so because these types can be internal types which makes
sense for only classes inside the assembly and not for external consumption.
But what about the type members and why 'internal' keyword at the type meber
level? - That is the crux of the question
"Sridhar Mahadevan" <sridharm@ctd.hcltech.com> wrote:
>
>Hi All,
>
>A nagging doubt.
>
>Why is there a need for access modifier named 'internal' in C# which is
partially
>borrowed from Java keyword "Protected"(Java is kind of confusing clubbing
>access to derived classes and package classes!).
>
>Apart from the literal meaning , that it can be accessed by types in the
>same assembly or Package(Java) , can somebody suggest a situation where
the
>addition of this keyword to C# language has justification.
>
>Me coming from C++ style of coding I can think this keyword as near to
'friend
>' keyword. But the keyword friend is used specifically by the class implementor/designer
>and has control over which classes to allow access. While in case of 'internal'
>the control shifts to the assembly/package designer as which classes has
>access depends on the classes inside the assembly/package. Why did the drafters
>of Java and C# language specification think of this as a nice idea?
>
>I can also give a parallel in 'C'. The keyword 'static' which restricts
the
>scope of the variable/method to the file in which the method or variable
>is declared. Were they thinking about this when they thought of the keyword
>'internal'.
>
>Is there anything similar to friend keyword in C# where I can limit access
>to only a certain set of classes and not all classes in the assembly.
>
>Please do not confuse with friend keyword in VB.NET. That is similar to
the
>keyword 'internal'.
>
>Thanks in advance
>Sridhar Mahadevan
-
Re: The 'internal' keyword.
You are correct that internal is similar to friend in C++.
Internal is generally used when you're creating a group of related classes
that need to be able to access the internals of each other. 'Internal"
really means 'public to those that I build with', which is a useful concept
when building libraries.
There is no concept of friend in .NET, though we've been talking about it a
bit. It's a bit complicated because it's hard to come up with a way of
specifying a friend that is good; you can't just say something like
"Utility.Checker" because anybody can create a class named that.
If you *really really* want to restrict access to a specific class, you can
do it by setting security on a method, but it's pretty ugly to do.
--
Visit the C# product team at http://www.gotdotnet.com/team/csharp
This posting is provided "AS IS" with no warranties, and confers no rights.
"Sridhar Mahadevan" <sridharm@ctd.hcltech.com> wrote in message
news:3d998b17$1@10.1.10.29...
>
> Hi All,
>
> A nagging doubt.
>
> Why is there a need for access modifier named 'internal' in C# which is
partially
> borrowed from Java keyword "Protected"(Java is kind of confusing clubbing
> access to derived classes and package classes!).
>
> Apart from the literal meaning , that it can be accessed by types in the
> same assembly or Package(Java) , can somebody suggest a situation where
the
> addition of this keyword to C# language has justification.
>
> Me coming from C++ style of coding I can think this keyword as near to
'friend
> ' keyword. But the keyword friend is used specifically by the class
implementor/designer
> and has control over which classes to allow access. While in case of
'internal'
> the control shifts to the assembly/package designer as which classes has
> access depends on the classes inside the assembly/package. Why did the
drafters
> of Java and C# language specification think of this as a nice idea?
>
> I can also give a parallel in 'C'. The keyword 'static' which restricts
the
> scope of the variable/method to the file in which the method or variable
> is declared. Were they thinking about this when they thought of the
keyword
> 'internal'.
>
> Is there anything similar to friend keyword in C# where I can limit access
> to only a certain set of classes and not all classes in the assembly.
>
> Please do not confuse with friend keyword in VB.NET. That is similar to
the
> keyword 'internal'.
>
> Thanks in advance
> Sridhar Mahadevan
-
Re: The 'internal' keyword.
Eric,
with that said, why not allow get/set to be modified individually? any
thoughts on allowing this in the future?
example:
public int ID {
get { return _id; }
}
internal int ID {
set { _id = value; }
}
or allow something a little tighter like:
int ID {
public get { return _id; }
internat set { _id = value; }
}
This is really useful, esp. when implementing a factory pattern.
-sean
"Eric Gunnerson" <ericgu_nospam@microsoft.nospam.com> wrote in message
news:3d99e5ca$1@10.1.10.29...
> You are correct that internal is similar to friend in C++.
>
> Internal is generally used when you're creating a group of related classes
> that need to be able to access the internals of each other. 'Internal"
> really means 'public to those that I build with', which is a useful
concept
> when building libraries.
>
> There is no concept of friend in .NET, though we've been talking about it
a
> bit. It's a bit complicated because it's hard to come up with a way of
> specifying a friend that is good; you can't just say something like
> "Utility.Checker" because anybody can create a class named that.
>
> If you *really really* want to restrict access to a specific class, you
can
> do it by setting security on a method, but it's pretty ugly to do.
>
> --
> Visit the C# product team at http://www.gotdotnet.com/team/csharp
>
> This posting is provided "AS IS" with no warranties, and confers no
rights.
>
> "Sridhar Mahadevan" <sridharm@ctd.hcltech.com> wrote in message
> news:3d998b17$1@10.1.10.29...
> >
> > Hi All,
> >
> > A nagging doubt.
> >
> > Why is there a need for access modifier named 'internal' in C# which is
> partially
> > borrowed from Java keyword "Protected"(Java is kind of confusing
clubbing
> > access to derived classes and package classes!).
> >
> > Apart from the literal meaning , that it can be accessed by types in the
> > same assembly or Package(Java) , can somebody suggest a situation where
> the
> > addition of this keyword to C# language has justification.
> >
> > Me coming from C++ style of coding I can think this keyword as near to
> 'friend
> > ' keyword. But the keyword friend is used specifically by the class
> implementor/designer
> > and has control over which classes to allow access. While in case of
> 'internal'
> > the control shifts to the assembly/package designer as which classes has
> > access depends on the classes inside the assembly/package. Why did the
> drafters
> > of Java and C# language specification think of this as a nice idea?
> >
> > I can also give a parallel in 'C'. The keyword 'static' which restricts
> the
> > scope of the variable/method to the file in which the method or variable
> > is declared. Were they thinking about this when they thought of the
> keyword
> > 'internal'.
> >
> > Is there anything similar to friend keyword in C# where I can limit
access
> > to only a certain set of classes and not all classes in the assembly.
> >
> > Please do not confuse with friend keyword in VB.NET. That is similar to
> the
> > keyword 'internal'.
> >
> > Thanks in advance
> > Sridhar Mahadevan
>
>
-
Re: The 'internal' keyword.
>with that said, why not allow get/set to be modified individually? any
>thoughts on allowing this in the future?
>
>example:
>
>public int ID {
> get { return _id; }
>}
>internal int ID {
> set { _id = value; }
>}
>
>or allow something a little tighter like:
>
>int ID {
> public get { return _id; }
> internat set { _id = value; }
>}
>
You can do this same thing just not with properties. You can rely on Get
and Set methods instead. Otherwise if you choose properties you could apply
security on the set part as Eric has stated. And I agree, it is ugly to
do.
-
Re: The 'internal' keyword.
along the same lines... i think the get/set method approach, is not
particularly prety.
Class.A = B;
is a lot more elegant than
class.SetA(B);
i'm not fanatical on it. in the long run, *maybe* it's a good thing. when
initializing and object i use a constructor with parameters for the whole
lot. probably a better performance alternative than setting the properties
individually.
but... it would sure be nice to have the ability.
"barknee@westrew.com" <none@none.com> wrote in message
news:3d9aef78$1@10.1.10.29...
>
> >with that said, why not allow get/set to be modified individually? any
> >thoughts on allowing this in the future?
> >
> >example:
> >
> >public int ID {
> > get { return _id; }
> >}
> >internal int ID {
> > set { _id = value; }
> >}
> >
> >or allow something a little tighter like:
> >
> >int ID {
> > public get { return _id; }
> > internat set { _id = value; }
> >}
> >
>
> You can do this same thing just not with properties. You can rely on Get
> and Set methods instead. Otherwise if you choose properties you could
apply
> security on the set part as Eric has stated. And I agree, it is ugly to
> do.
-
Re: The 'internal' keyword.
We talked about this a fair bit when we did properties in the first place.
For a while, we had the option to be able to have a normal setter and a
virtual getter (though we never had different accessibilities). The added
complexity was pretty confusing, as it was hard to remember what modifiers
you could put on a property, what ones could go on an accessor, and what
ones were permitted in either location. After a bunch of user reports from
the frameworks team, we decided that the added compexity just wasn't worth
it, and elected to simplify the model.
It turned out to be a very important simplification, and made properties
much easier to use.
The same arguments apply to accessibility. If you have different
accessibilities, a property doesn't have a user model like a field any more;
it has a more complex one. We do force you to use a workaround if you want
to do this, but a bit more complexity for the framework author is worth less
complexity for the user of the class
--
Visit the C# product team at http://www.gotdotnet.com/team/csharp
This posting is provided "AS IS" with no warranties, and confers no rights.
"Sean Newton" <snewton@streamlineic.com> wrote in message
news:3d9a1009$1@10.1.10.29...
> Eric,
>
> with that said, why not allow get/set to be modified individually? any
> thoughts on allowing this in the future?
>
> example:
>
> public int ID {
> get { return _id; }
> }
> internal int ID {
> set { _id = value; }
> }
>
> or allow something a little tighter like:
>
> int ID {
> public get { return _id; }
> internat set { _id = value; }
> }
>
> This is really useful, esp. when implementing a factory pattern.
>
> -sean
>
> "Eric Gunnerson" <ericgu_nospam@microsoft.nospam.com> wrote in message
> news:3d99e5ca$1@10.1.10.29...
> > You are correct that internal is similar to friend in C++.
> >
> > Internal is generally used when you're creating a group of related
classes
> > that need to be able to access the internals of each other. 'Internal"
> > really means 'public to those that I build with', which is a useful
> concept
> > when building libraries.
> >
> > There is no concept of friend in .NET, though we've been talking about
it
> a
> > bit. It's a bit complicated because it's hard to come up with a way of
> > specifying a friend that is good; you can't just say something like
> > "Utility.Checker" because anybody can create a class named that.
> >
> > If you *really really* want to restrict access to a specific class, you
> can
> > do it by setting security on a method, but it's pretty ugly to do.
> >
> > --
> > Visit the C# product team at http://www.gotdotnet.com/team/csharp
> >
> > This posting is provided "AS IS" with no warranties, and confers no
> rights.
> >
> > "Sridhar Mahadevan" <sridharm@ctd.hcltech.com> wrote in message
> > news:3d998b17$1@10.1.10.29...
> > >
> > > Hi All,
> > >
> > > A nagging doubt.
> > >
> > > Why is there a need for access modifier named 'internal' in C# which
is
> > partially
> > > borrowed from Java keyword "Protected"(Java is kind of confusing
> clubbing
> > > access to derived classes and package classes!).
> > >
> > > Apart from the literal meaning , that it can be accessed by types in
the
> > > same assembly or Package(Java) , can somebody suggest a situation
where
> > the
> > > addition of this keyword to C# language has justification.
> > >
> > > Me coming from C++ style of coding I can think this keyword as near
to
> > 'friend
> > > ' keyword. But the keyword friend is used specifically by the class
> > implementor/designer
> > > and has control over which classes to allow access. While in case of
> > 'internal'
> > > the control shifts to the assembly/package designer as which classes
has
> > > access depends on the classes inside the assembly/package. Why did the
> > drafters
> > > of Java and C# language specification think of this as a nice idea?
> > >
> > > I can also give a parallel in 'C'. The keyword 'static' which
restricts
> > the
> > > scope of the variable/method to the file in which the method or
variable
> > > is declared. Were they thinking about this when they thought of the
> > keyword
> > > 'internal'.
> > >
> > > Is there anything similar to friend keyword in C# where I can limit
> access
> > > to only a certain set of classes and not all classes in the assembly.
> > >
> > > Please do not confuse with friend keyword in VB.NET. That is similar
to
> > the
> > > keyword 'internal'.
> > >
> > > Thanks in advance
> > > Sridhar Mahadevan
> >
> >
>
>
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks