-1

Can I access the internal variables outside the class?

If I have an internal variable like this:

Class Example    
{
    internal string CommonHome = "C:/myfile/";
}

How to access this CommonHome from outside the class or another class?

There is any possible way for accessing this?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Ajar
  • 162
  • 1
  • 10
  • 2
    possible duplicate of [Internal vs. Private Access Modifiers](http://stackoverflow.com/questions/3813485/internal-vs-private-access-modifiers) – Matt Smith Feb 06 '15 at 05:29
  • 3
    Why are you marking it internal if you want to expose it outside – Vimal CK Feb 06 '15 at 05:30

2 Answers2

3

internal variable are accessible in current dll only.

You can have look at msdn for this => link

Here is some code snippets from there.

public class BaseClass 
{
   internal static int intM = 0;
}

public class TestAccess 
{
   static void Main() 
   {
      BaseClass myBase = new BaseClass();   // Ok.
      BaseClass.intM = 444;    // CS0117
   }
}
Jenish Rabadiya
  • 6,708
  • 6
  • 33
  • 62
2

You can access the internal members within the assembly you declared them in other classes. Since your inter data member is instance member you need to create instance of Example in a class you want CommonHome

Example  ex = new Example();
string s = ex.CommonHome;

You probably need to give type of CommonHome which is string I guess

Class Example  
{    
    internal string CommonHome = "C:/myfile/";    
}

You better use property for CommonHome instead of public data member as you may need to apply some business rule later to this. This is a very good post about the properties, Why Properties Matter.

Class Example  
{    
    internal string CommonHome { get; set; }
    internal Example()
    {
       CommonHome = "C:/myfile/"; 
    }
}

The internal keyword is an access modifier for types and type members. Internal types or members are accessible only within files in the same assembly, MSDN.

You can read more about access modifiers here.

Adil
  • 146,340
  • 25
  • 209
  • 204