简体   繁体   中英

Why public variables are not allowed inside a method or any block except class in c#?

A very strange issue I faced that when I try to declare a variable say of type string with public keyword inside a method of a class then a compile-time error is being thrown by the compiler with no proper explanation.

Consider below piece of code:

public class MyClass
{
    public string publicVarInClass= null;  // No error here. Works fine

    public void MyMethod()
    {    // Error shown here is "Expected } "
        public string publicVarInMethod = null;  // not allowed, but no error is shown in this line. WHY?

        try // Error shown here is Invalid token 'try' in class, struct or interface declaration
        {
        //some code here
        }
    }
}

According to my consideration when we decalre a variable as public inside a method, it excepts that method to be a class, struct or interface.

And that's what my question is that why C# is restricting us to to declare a variable as public inside method. What's the problem with it?

There is a difference between variables and fields. Only fields have access protection, while variables cannot be accessed at all outside method's body.

"Public variables" in C# are called fields . C# allows field declarations to appear only in the body of a class or a struct. Fields are parts of an object definition, which are either attached to an instance, or shared among all instances.

Variables inside methods, on the other hand, are not fields. Being local to a method body, they do not need access protection. Their lifetime and scope are limited inside the method, so there is no way for any code outside of that method to access local variables.

This is to do with scope, we don't give variables an access modifier within the scope of a Method, the variable is only accessible in that method and no where else, we add access modifiers(public private protected) to properties/fields of a class.

For reference Public has global availability from other objects Private can only be accessed within that class or objects of that class Protected is accessible from within that class or child classes

Inside a method any variable you declare is only accessible within the method, it is called a local variable. Access modifiers (public, private etc.) make no sense here, as the variable won't exist outside the scope of the method.

If you want a method to set up your public variable for access elsewhere, you can assign values to it, but you can't declare a public variable in your method.

First answear yourself a question - how would you like to access this variable outside of the method?

What you are asking is the basics of OOP. Classes has fields/properties and methods, that operates on those fields.

Because this variable lifetime begins and ends only when method starts and ends. Program don't see variables inside methods.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM