function returning a const value

I have a following function which accepts a enum value and based upon the enum value returns a constant value(which is in a different class). Now i get a "Constant initializer missing" Error.

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;
    }
}
  1. how exactly the function's return type would be? (I am using object return type and this does not throw any error)

  2. Is there any other option to achieve the same?

Upvotes: 5

Views: 17526

Answers (4)

Eric Lippert
Eric Lippert

Reputation: 660138

What's happening here is the compiler thinks that you are trying to declare a public constant integer field named Testfunction, and is extremely surprised to discover that there is no = 123; following the identifier. It's telling you that it expected the initializer.

There are a number of places in the C# compiler where the development team anticipated that C and C++ users would use a common C/C++ syntax erroneously. For example, there's a special error message for int x[]; that points out that you probably meant int[] x;. This is an example of another place where the compiler could benefit from a special-purpose error message that detects the common mistake and describes how to fix it. You might consider requesting that feature if you think it's a good one.

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

Upvotes: 27

Alvin Wong
Alvin Wong

Reputation: 12430

The 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# Reference):

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

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


So you are from the great C/C++ kingdom. Thinking with my very limited knowledge to C/C++, const return types in C/C++ is only meaningful with pointers...

// C++ code
const int m = 1;

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

But unless you are using unsafe code, there is no pointer in C#. There are only value types (like int/DateTime/TestEnum/structs) and reference types (like string/classes). There are a lot more to read about on the web.

So as int is a value type in C#, when you returns it, it gets copied. So even if you are returning a "constant int" the returned value is not a constant and modifying the return value will not "change the constant and cause a SegFault".


Well, I forgot to answer your questions...

  1. How exactly the function's return type would be? (I am using object return type and this does not throw any error)

    Like what I showed in the code above, int.
    const int blah = 1 only declares a variable/field blah of type int which one must not modify it (by doing blah = 2). In C# const int is not a type.

  2. Is there any other option to achieve the same?

    Well... I think I don't actually need to answer this...

Upvotes: 3

Pawan Nogariya
Pawan Nogariya

Reputation: 8960

Removing "const" keyword from your function return type should solve the problem

It should be like this

public int Testfunction(TESTENUM TestEnum)
{
    ...

Return type cannot be declared constant

Upvotes: 4

Mitch Wheat
Mitch Wheat

Reputation: 300559

.NET methods cannot return const any type..

The const keyword is invalid there: remove it.

Upvotes: 2

Related Questions