문제

Is there any way to limit the scope of a compile-time constant to a method/block of code that avoids hardcoding the value?

Something like the following:

public int SomeMethod(int a) {
    const int SomeCompileTimeConstant = 10; // obviously this doesn't exist

    return a + SomeCompileTimeConstant;
}

opposed to hardcoding the value:

public int SomeMethod(int a) {              
    return a + 10;
}

or making it a class-level constant:

public class A {
    private const int SomeCompileTimeConstant = 10;

    public int SomeMethod(int a) {
        return a + SomeCompileTimeConstant;
    }
}
도움이 되었습니까?

해결책

You can declare a constant inside a function.

When you wrote

public int SomeMethod(int a) {
    const int SomeCompileTimeConstant = 10; // obviously this doesn't exist

    return a + SomeCompileTimeConstant;
}

you were wrong.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top