Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In C#, is it possible to restrict a class accessibility to the file defining it?

Tags:

c#

I can't declare it as private in another class because I have several classes that use it. I also don't want it to be internal otherwise it will be exposed to other classes in the same assembly. I want it to be accessible just to the classes in the same file. Is it possible in C#?

like image 953
jimmy Avatar asked Feb 18 '13 06:02

jimmy


People also ask

What does << mean in C?

<< is the left shift operator. It is shifting the number 1 to the left 0 bits, which is equivalent to the number 1 .

What does %d do in C?

In C programming language, %d and %i are format specifiers as where %d specifies the type of variable as decimal and %i specifies the type as integer. In usage terms, there is no difference in printf() function output while printing a number using %d or %i but using scanf the difference occurs.

What is && operator in C?

The && (logical AND) operator indicates whether both operands are true. If both operands have nonzero values, the result has the value 1 . Otherwise, the result has the value 0 . The type of the result is int . Both operands must have an arithmetic or pointer type.


1 Answers

There's very little in the C# language which is done at at the level of "source file".

The closest you could come would be to create a top-level class and have several nested classes:

class Foo
{
    internal class A
    {
        private Shared shared = new Shared();
    }

    internal class B
    {
        private Shared shared = new Shared();
    }

    private class Shared
    {
    }
}

It's not a very pleasant solution though, to be honest. If a class needs to be visible to a number of other classes, I'd typically prefer to make it internal and either extract out those classes to another assembly or live with the shared class being visible to other clsses in the same assembly which don't really need to know about it.

like image 197
Jon Skeet Avatar answered Oct 26 '22 23:10

Jon Skeet