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...
Internal variableWhats its use
Page 1 of 1
2 Replies - 3034 Views - Last Post: 21 March 2010 - 09:12 AM
Replies To: Internal variable
#2
Re: Internal variable
Posted 21 March 2010 - 09:05 AM
My understanding is that an internal variable can only be accessed by the code file it is declared in.
For example:
Main.cs
IWannaUsemyInt.cs
So, internal variables are really local, no other file can access it. You can also make classes internal instead of public or private.
For example:
Main.cs
//compile as a DLL
public class MainClass
{
internal static int myInt = 10;
}
IWannaUsemyInt.cs
//reference Main.dll
public class UseMyIntNow
{
static void Main()
{
MainClass myNewMainClass = new MainClass(); //It's okay with this.
MainClass.myInt = 144; //It is not allowed to access my int so it gives an error.
}
}
So, internal variables are really local, no other file can access it. You can also make classes internal instead of public or private.
#3
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.
Not quite. It is namespace wide. If you are in the same namespace you can use internal.
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.
Page 1 of 1
|
|

New Topic/Question
Reply




MultiQuote





|