function returning a const value
Solution 1
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
Solution 2
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.)
Solution 3
.NET methods cannot return const
any type..
The const
keyword is invalid there: remove it.
Solution 4
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...
-
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/fieldblah
of typeint
which one must not modify it (by doingblah = 2
). In C#const int
is not a type. -
Is there any other option to achieve the same?
Well... I think I don't actually need to answer this...
Related videos on Youtube
now he who must not be named.
#SOreadytohelp There is this joy in helping others. Thanks SO
Updated on June 25, 2022Comments
-
now he who must not be named. almost 2 years
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; } }
how exactly the function's return type would be? (I am using object return type and this does not throw any error)
Is there any other option to achieve the same?
-
Alvin Wong over 11 yearsYou're from the world of C/C++, right? There aren't something like
const char[]
in C#.
-
now he who must not be named. over 11 years"Return type cannot be declared constant": Is it a rule in C# ?
-
Pawan Nogariya over 11 yearsI infer it as a rule from the definition given for "const" keyword by microsoft i.e. "The const keyword is used to modify a declaration of a field or local variable." so here we are neither using it with the field nor with the local variable. Secondly const means something that should be known during compilation time. But function's return value will be decided at run time
-
now he who must not be named. over 11 yearsThanks Eric for detailed explanation!
-
Valentino Miori about 2 yearsBTW it makes little to no sense (even in C++) to return a const value, especially a builtin, since it gets copied anyway or copy-elided and guaranteed to not mutate anything.