2 Replies - 4063 Views - Last Post: 21 March 2010 - 09:12 AM Rate Topic: -----

#1 mahesh likhe  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 24
  • Joined: 21-March 10

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...
Is This A Good Question/Topic? 0
  • +

Replies To: Internal variable

#2 adgarci  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 96
  • Joined: 26-August 09

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
//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.
Was This Post Helpful? 0
  • +
  • -

#3 SixOfEleven  Icon User is offline

  • using Caffeine;
  • member icon

Reputation: 945
  • View blog
  • Posts: 6,342
  • Joined: 18-October 08

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.

View Postadgarci, 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.
Was This Post Helpful? 2
  • +
  • -

Page 1 of 1