tags:

views:

55

answers:

3

I have many many functions (100+ estimated) and more then half are only needed in the file its defined. Is there a way to define functions so it scope is at a file level?

+2  A: 

No. There's no way to restrict access to a single "file". You can put all the functions in a single class and make the ones you want to protect private. Alternatively, you may put all the functions inside another assembly and make the protected ones internal.

Mehrdad Afshari
Any idea if i can somehow create a private class and have `this` pointer access everything the outer class can access? (probably not)
acidzombie24
Inner classes can access private members in outer classes. That's yet another solution. It doesn't look much different from just making the methods private in the outer class directly to me.
Mehrdad Afshari
hmm, i see that an inner class can access the outer class *static* members. But it doesnt look like it can access `this`. This doesnt seem to work the way i am hoping it would. To access everything that the other function can with this. Copy/pasting a function into the inner class causes many compile errors. I understand why it doenst work but i was hoping it would work.
acidzombie24
No, it's not like Java. But you can easily pass an instance to `this` to the nested class constructor.
Mehrdad Afshari
A: 

You can restrict scope to an assembly using internal, but not to file level.

Mitch Wheat
+2  A: 

Firstly; C# has methods, not functions ;-p

Methods are scoped to the type; if you are trying to restrict to a file, can I therefore assume that this is a partial class (or maybe a set of nested types) that is (/are) split over multiple files? If not (and it is a single file) just make the methods private. If it is currently partial, then no: there is no way to avoid cross-calling between different files that are the same type. You could create some nested static types per partial portion, but that seems ugly, hacky, and just wrong.

If it isn't actively hurting you - perhaps don't change it?

Marc Gravell
@Marc, the other possibility is that the OP is talking about _file_ because he doesn't understand OOP.
John Saunders
Yes it is a partial class. I guess i am just worried if i give have someone else maintain the code (its a personal project and a nifty site) he will become very afraid. Maybe not while reading code but when making changes and looking for the correct function.
acidzombie24