473,320 Members | 1,832 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Inconsistent access levels???

I've run into something that doesn't make a lot of sense and I'd appreciate
if someone could explain the logic.

Let's say I have:

internal interface MyInterface
{
void foo();
}

internal class MyClass : MyInterface
{
internal void foo()
{
Bar();
}
}

I get an error because the implementation of foo() is internal and should be
public.

What's the logic behind that? The class is internal, so isn't foo(), for all
intents and purposes, internal?

What am I missing?

Pete
Nov 16 '05 #1
7 1284
> internal class MyClass : MyInterface
{
internal void foo()
{
Bar();
}
}

I get an error because the implementation of foo() is internal and should be public.


MyClass is already internal, so foo() doesn't need to be. There's no way
anything outside of the assembly can have access to MyClass, and that
includes the foo() function. making it public will not allow it to be
accessed from elsewhere.

Chris
Nov 16 '05 #2
Yes, I realize that but isn't that a bit inconsistent? I mean, if the class
is internal, the method is effectively internal, why not allow or even
enforce, that the methods be internal?

The methods are "more accessible" than the class which just seems to defy
sense, but maybe there's a reason for it. If there is, I guess I'm trying to
understand the reason.

Pete
"Chris" <ct******@hotmail.com> wrote in message
news:ef*************@TK2MSFTNGP12.phx.gbl...
internal class MyClass : MyInterface
{
internal void foo()
{
Bar();
}
}

I get an error because the implementation of foo() is internal and
should be
public.


MyClass is already internal, so foo() doesn't need to be. There's no way
anything outside of the assembly can have access to MyClass, and that
includes the foo() function. making it public will not allow it to be
accessed from elsewhere.

Chris

Nov 16 '05 #3
> The methods are "more accessible" than the class which just seems to defy
sense, but maybe there's a reason for it. If there is, I guess I'm trying to understand the reason.


I see what you mean, but I guess that's just the way it is. look at this:

public class A
{
private class B
{
public int x;
private int y;
}

public void foo()
{
B b = new B();
b.x = 1;
b.y = 2; // COMPILE ERROR
}
}

it makes sense to me that they would do it this way because of what I just
demonstrated. the B class is private, but it's private members shouldn't be
available to A. I'm assuming they did something similar with 'internal'
just to be consistent.

Chris
Nov 16 '05 #4
<pd******@hotmail.com> wrote:
Yes, I realize that but isn't that a bit inconsistent? I mean, if the class
is internal, the method is effectively internal, why not allow or even
enforce, that the methods be internal?


No, the class *isn't* effectively internal.

Consider the following files:

Assembly1.cs:

using System;

public interface IFoo
{
void Bar();
}

public class Factory
{
public static IFoo GetFoo()
{
return new FooImpl();
}
}

internal class FooImpl : IFoo
{
public void Bar()
{
Console.WriteLine ("Hello");
}
}

Assembly2.cs:
public class Driver
{
static void Main()
{
Factory.GetFoo().Bar();
}
}

Now compile:

csc /target:library Assembly1.cs
csc /r:Assembly1.dll Assembly2.cs

Now run:

Assembly2

prints

Hello

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #5
But not if interface IFoo is declared internal! That's my point.

Pete

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
<pd******@hotmail.com> wrote:
Yes, I realize that but isn't that a bit inconsistent? I mean, if the class is internal, the method is effectively internal, why not allow or even
enforce, that the methods be internal?


No, the class *isn't* effectively internal.

Consider the following files:

Assembly1.cs:

using System;

public interface IFoo
{
void Bar();
}

public class Factory
{
public static IFoo GetFoo()
{
return new FooImpl();
}
}

internal class FooImpl : IFoo
{
public void Bar()
{
Console.WriteLine ("Hello");
}
}

Assembly2.cs:
public class Driver
{
static void Main()
{
Factory.GetFoo().Bar();
}
}

Now compile:

csc /target:library Assembly1.cs
csc /r:Assembly1.dll Assembly2.cs

Now run:

Assembly2

prints

Hello

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #6
I think that's a different situation, though. A class can only be private if
it's contained within another class. There's not really anything comparable
because a class within a namespace can only be public or internal.

Pete
"Chris" <ct******@hotmail.com> wrote in message
news:ez**************@TK2MSFTNGP11.phx.gbl...
The methods are "more accessible" than the class which just seems to defy sense, but maybe there's a reason for it. If there is, I guess I'm
trying to
understand the reason.
I see what you mean, but I guess that's just the way it is. look at this:

public class A
{
private class B
{
public int x;
private int y;
}

public void foo()
{
B b = new B();
b.x = 1;
b.y = 2; // COMPILE ERROR
}
}

it makes sense to me that they would do it this way because of what I just
demonstrated. the B class is private, but it's private members shouldn't

be available to A. I'm assuming they did something similar with 'internal'
just to be consistent.

Chris

Nov 16 '05 #7
<pd******@hotmail.com> wrote:
But not if interface IFoo is declared internal! That's my point.


Ah, apologies. I hadn't seen that the interface was internal as well as
the class itself.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Bill Willyerd | last post by:
Hello All, I have been searching for a published document for Best Practices concerning access levels based on roles. Should developers have more than (if at all) select level access to...
14
by: Sean C. | last post by:
Helpful folks, Most of my previous experience with DB2 was on s390 mainframe systems and the optimizer on this platform always seemed very predictable and consistent. Since moving to a WinNT/UDB...
42
by: lauren quantrell | last post by:
So many postings on not to use the treeview control, but nothing recently. Is it safe to swim there yet with Access 2000-Access 2003?
0
by: Namratha Shah \(Nasha\) | last post by:
Hey Guys, Today we are going to look at Code Access Security. Code access security is a feature of .NET that manages code depending on its trust level. If the CLS trusts the code enough to...
0
by: William F. Zachmann | last post by:
A web site that will run on Windows Server 2003 and IIS 6.0 needs to provide three levels of access, one for the public and two others for two levels of subscribers. This is a port of a prior site...
3
by: ukrutter | last post by:
Any help would be greatfully recieved here! I'll try and explain the situation here... I have a database in Access, which is in essence a booking system. I am using VB6 to link to this database....
35
by: Francine.Neary | last post by:
I'm finding it really hard to keep some of these things straight... For example, fputs is to puts as fprintf is to printf, except that fputs has the file handle at the end and fprintf at the...
4
by: carl.dhalluin | last post by:
Hello I am completely puzzled why the following exec code does not work: mycode = "import math\ndef f(y):\n print math.floor(y)\nf(3.14)" def execute(): exec mycode execute()
3
by: Rahul Babbar | last post by:
Hi All, When could be the possible reasons that could make a database inconsistent? I was told by somebody that it could become inconsistent if you " force all the applications to a close on...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.