Why is the const variable available in the static method?

I write code without understanding WHY I can access constant values ​​in static methods.

Why is it possible to access values constwithout declaring it as static?

For example.) This right to name IMAGE_FILE_EXTENSIONwithinAddImageToDocument(...)

public abstract class ImageDocumentReplacer : DocumentReplacer
{
    private const string IMAGE_FILE_EXTENSION = ".tif";

    private static void AddImageToDocument(int documentId, string separatedPath)
    {
        Console.WriteLine(IMAGE_FILE_EXTENSION);
    }
}
+5
source share
3 answers

constmembers are implicit static. They belong to a class, not a specific instance. As a result, you cannot use this.myConstant, but MyClass.myConstant.

Specification Specification C # 3.0 (Section §10.4 Constants):

static , - a static. , .

+17

? , ( readonly, )

+2

I would expect that since a constant cannot change instance-instance, this makes them safe to access from a static method.

+1
source

All Articles