Search code examples
c#classvariablesstatic

Calling a Variable from another Class


How can I access a variable in one public class from another public class in C#?

I have:

public class Variables
{
   static string name = "";
}

I need to call it from:

public class Main
{
}

I am working in a Console App.


Solution

  • That would just be:

     Console.WriteLine(Variables.name);
    

    and it needs to be public also:

    public class Variables
    {
       public static string name = "";
    }