Whats its use

Internal variable

Posted 21 March 2010 - 08:51 AM

What is the use of Internal variable???
I know that internal variable is accessible in assembly but what its actual use. Means can any one tell me where can i use this type of variable. Any practical example...

Re: Internal variable

Posted 21 March 2010 - 09:12 AM

Internal is best used when the variable, method, property, etc would be needed by classes inside of your assembly that you don't want accessed outside of your assembly. It offers a layer of protection to your data and helps with encapsulation, and OOP principle.

When designing your classes you should try to make them so that others using your classes do not need to know how your classes work. They should just know what the class does, not how it does it. By giving your classes a secure public interface (not talking C# interfaces) to access data in your class you reduce the chances of somebody who is using your class the ability to change things inside of your class that shouldn't be changed. You should try and only make types that are not reference types public outside of your assembly. Making a class, which is a reference type, public means that others can change things inside of your class that you probably do not want changed. It goes back to the idea of encapsulation. Keeping your data private and having people use your classes with a safe public interface that will not change critical data.

Sorry but I don't really have a practical example at the moment. Just the theory involved but hope this helps you a little.

adgarci, on 21 March 2010 - 11:05 AM, said:

My understanding is that an internal variable can only be accessed by the code file it is declared in.

Not quite. It is namespace wide. If you are in the same namespace you can use internal.