0
I had declared a variable in a method. how can I call the variable in the main method?
5 odpowiedzi
+ 4
A variable declared within a method can only be accessed within that method and nowhere else. The scope of the variable cannot go beyond the method its declared in.
+ 2
You can't call it in the main if you declare it in a method, you could declare it in the main and use it in the method (if you have to modify the variable you have to use it as a reference or it won't change after finishing the method ).
Hope it helps
0
the best think to do is to declare the variable private in the class and then you could just get or set the value in the whole class where you want
0
For example:
class Example {
private static string variable;
static void main (string [] args){
yourMethod();
Console.WriteLine (variable);
}
private static void yourMethod() {
variable = "test";
}
}
0
owh. thanksss