The internal key word does not work as a book say.

T

Tony Johansson

Hello!

I want to say that I rather new to C# have only worked with it for 5 month.

I'm now reading a book and there are some example on the internal key work
that doesn't work
as the book say. I just want to know if the book is right or wrong.

I have two files with two classes called Class1 and Class2.
The first Class Class1 is located in namespace AccessControl and the second
class
Class2 is located in namespace Test. You can see the classes below.
Class2 has the internal key word on both the class and the constructor and
on the method A_internal().
I can instansiate Class2 from Class1 without any problem and also call the
A_internal() metod that exist in Class2 from Class1 without any problem.

Can somebody explain how this internal work?


//Here is file 1 with name Class1.cs
using System;
using Test;

namespace AccessControl
{
public class Class1
{
public Class1() {} //constructor
public static int Main(string[] strings)
{
Class2 ba = new Class2();
return 0;
}
}
}

//Here is file 2 with name class2.cs
using System;
namespace Test
{
internal class Class2
{
internal Class2() {} //Constructor

internal void A_internal()
{
Console.WriteLine("Class2.A_internal");
}
}
}

Many thanks

//Tony
 
J

Jon Skeet [C# MVP]

Tony Johansson said:
I want to say that I rather new to C# have only worked with it for 5 month.

I'm now reading a book and there are some example on the internal key work
that doesn't work
as the book say. I just want to know if the book is right or wrong.

I have two files with two classes called Class1 and Class2.
The first Class Class1 is located in namespace AccessControl and the second
class
Class2 is located in namespace Test. You can see the classes below.

Does your book definitely say that "internal" is to do with namespaces?
It's not - it's to do with assemblies. Two types which are in different
assemblies can't see each others' internal members. (And a type can't
see internal types in a different assembly.)
 
T

Tom Porterfield

Tony said:
Hello!

I want to say that I rather new to C# have only worked with it for 5
month.
I'm now reading a book and there are some example on the internal key work
that doesn't work
as the book say. I just want to know if the book is right or wrong.

Classes and methods marked as internal are available to other classes and
methods in the same assembly. Namespace is irrelevant in this context. You
can have multiple namespaces in the same assembly, and the same namespace in
mulitple assemblies. But the internal modifier means that anything marked
as such is accessible within the same assembly, but inaccessible outside the
assembly.

Re-read that section of the book and see if it isn't saying that internal
scopes an item to the assembly, rather than to the namespace.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top