How do I create global variables?

时间:2016-02-12 19:20:40

标签: c# variables

Just started out, I'm purely confused about variable scopes. Why won't this work? How do I make this work? Could anyone possibly help me? Why doesn't the variable work in the main method? [I'm already assured that this question will get reported/locked for some unknown reason that I don't know about]

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TestingC
{
    public class Program
    {
        public void First()
        {
            Console.WriteLine("Apple is: " + apple);
        }

        static void Main(string[] args)
        {
            public const string apple = "good!";
            Console.WriteLine("Apple is: " + apple);
            Console.ReadLine();
            First();
            Console.WriteLine("Apple is: " + apple);
            Console.ReadLine();

        }
    }
}

5 个答案:

答案 0 :(得分:3)

The simple answer, is that the "Brackets" define the scope.

Any time you declare a variable, if you want to know where you can use it, find out what set of brackets it is inside. In your case, apple only applies to Main. First cannot see it, because it is declared at a different level. Only the things that are also inside the same set of brackets as a variable can see it.

Also note, its the FIRST set of brackets, going outward from the variable. So starting at the declaration for apple, you go out to the brackets around main. That describes the scope of apple.

答案 1 :(得分:1)

this will do that bro!

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TestingC
{
public class Program
  {
    public const string apple = "good!";
    public static void First()
    {
        Console.WriteLine("Apple is: " + apple);
    }

    static void Main(string[] args)
    {
        Console.WriteLine("Apple is: " + apple);
        Console.ReadLine();
        First();
        Console.WriteLine("Apple is: " + apple);
        Console.ReadLine();

    }
  }
}

答案 2 :(得分:0)

Your variable apple is defined within Main. You can only use it inside that method. To use it inside the First() method, you need to declare apple at the class level.

Put public static const string apple = "good!";

just below public class Program {

I suggest you do some reading about scope of variables. True global variables don't exist in C#.

EDIT: Added static, as pointed out by Steve.

答案 3 :(得分:0)

The variable is only visible from the scope you declared it in, which in this case is in the Main method. Generally, in C# (and in mot other C family languages), that means between the curly brackets. Your apple var would also be visible in nested scope (another pair of brackets {} inside Main). If you want a variable to be visible in both your methods, you have to declare it as a member of the Program class (inside the class but outside of any method)

答案 4 :(得分:0)

As an answer to:

"EDIT: How do I create a variable inside a function that can be accessed by other functions?"

(which is no longer there, but I think was you clarifying your question)

The short answer is that you can't, at least not in the way you're implying.

Variables defined in a method are local variables, held on the stack frame, meaning they can only be accessed within the method, unless they are passed into another method as a variable, or back out of this method as the return value, or as an out or ref parameter.

You could pass the string as a parameter to the First method.

So, change the First method to have a parameter:

public static void First(string stringToPassIn)
{
    ...
}

And call it with the argument:

First(apple);

You would acces it in the method with stringToPassIn then.

You should probably learn about passing by reference and passing by value, for reference types and value type, respectively.

See this for starters https://msdn.microsoft.com/en-us/library/ms173114.aspx

If you wanted something declared in First to be accessible in Main, you could use out or ref parameters, depending on your needs.

Again, learning about methods in C# would be a good idea