Function returning const value

I have the following function that takes an enumeration value and, based on the enum value, returns a constant value (which is in another class). Now I get the error "Constant Initializer".

public const int Testfunction(TESTENUM TestEnum) { switch(TestEnum) { case TestEnum.testval1: return testclass.constvalue; case TestEnum.testVal2: return testclass.constvalue1; case TestEnum.testVal3: return testclass.constvalue2; } } 
  • How exactly will the return type of the function look like? (I use the return type of the object and this does not cause any error)

  • Is there any other option to achieve the same?

+4
source share
4 answers

Removing the const keyword from the returned function type should solve the problem

It should be like

 public int Testfunction(TESTENUM TestEnum) { ... 

Return type cannot be declared constant

+5
source

What happens here, the compiler thinks you're trying to declare a public integer field called Testfunction, and is very surprised to find that there is no = 123; after the identifier = 123; . This tells you that he was expecting an initializer.

There are several places in the C # compiler where the development team expected C and C ++ users to use the common C / C ++ syntax erroneously. For example, there is a special error message for int x[]; , which indicates that you probably meant int[] x; . This is an example of another place where the compiler could use a special error message that detects a common error and describes how to fix it. You might consider requesting this feature if you think it is good.

More generally, "const" in C # means something different than what it means in C or C ++. C # does not have the concept of a “const function” that does not mutate state, or a “const reference” that provides a read-only representation of potentially mutable data. In C #, "const" is used only to declare that a field (or local) should be considered as a compile-time constant. (And "readonly" is used to declare that the field should be written only in the constructor, which is also quite useful.)

+22
source

.NET methods cannot return const any type.

The const keyword is not valid: delete it.

+2
source

Most likely working code:

 public static class TestClass { public const int constValue1 = 1; public const int constValue2 = 2; public const int constValue3 = 3; } enum TestEnum { testVal1, testVal2, testVal3 } public int TestFunction(TestEnum testEnum) { switch (testEnum) { case TestEnum.testVal1: return TestClass.constValue1; case TestEnum.testVal2: return TestClass.constValue2; case TestEnum.testVal3: return TestClass.constValue3; } return 0; // all code paths have to return a value } 

First, according to const (C # link) :

The const keyword is used to change the declaration of a field or local variable. It indicates that the value of a field or local variable is constant, which means that it cannot be changed.

In C #, const is only used as a field modifier (e.g. TestClass.constValue1 ) or a local variable, not suitable for the return type of a function.


So, you are from the great kingdom of C / C ++. Thinking with my very limited knowledge of C / C ++, the returned const types in C / C ++ only make sense with pointers ...

 // C++ code const int m = 1; // It returns a pointer to a read-only memory const int* a(){ return &m; } 

But if you use unsafe code, there is no pointer to C # . There are only value types (e.g. int / DateTime / TestEnum / structs) and reference types (e.g. string / classes). There is more to read on the Internet.

Since int is a value type in C #, when you return it, it gets copied . Therefore, even if you return an int constant, the return value is not a constant, and changing the return value will not "change the constant and call SegFault".


Ok, I forgot to answer your questions ...

  • How exactly will the function type be returned? (I use the return type of the object and this does not cause any error)

    As shown in the above code, int .
    const int blah = 1 declares only a variable / field of blah type int , which it should not change (by making blah = 2 ). In C #, const int not a type.

  • Is there any other option to achieve the same?

    Well ... I guess I really don't need to answer this question ...

+2
source

All Articles