Anyone out there any ideas why this compiles and runs?
I'm using Visual C# 2008.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        public enum fluf_value
        {
            thegood,
            thebad,
            theugly,
        };

        static fluf_value MyFunc()
        {
            return fluf_value.theugly;
        }

        static void Main(string[] args)
        {
            MyFunc();
            Console.ReadKey();
        }
    }
}

Is MyFunc returning something into the twilight zone?
Is there a compiler option to inhibit this behavior?
Any help is more than welcome.

Recommended Answers

All 11 Replies

Just because a function returns something it isnt always required you want it.. While I havent checked Im fairly sure you can do so with most functions.. in fact things like

String s = "this is a test";
s.Replace("s","5");

Doesnt produce the desired effect because it returns a string, but the code didnt make use of it - but it compiles and runs :)

Come to think of it. In C the main function normally returns some sort of errcode to the OS but in C# it is not needed in some way.
I'm actually writing an expression evaluator sort of thing(a bit of a hobby :icon_cool: ) and was depending on the returnvalue of a function which, as in my codesnippet, returned nothing of course.
Is it something that all C-like languages have in common?

I believe so, its not something I gave much consideration to, Although I do know for example youc ould argue that one of the things in C was that you could do things such as

if (a = b) { .. }

which worked if a got set to b and is still valid in c#, so you could argue a lot of return values arent used as the boolean in that case isnt used in a single line of

a= b;

Oh - you didnt know that?

First of all thanks to you all for your attention!
To LizR :
When I type if(a=b) I get an error which is great in C# an assignment isn't a boolean, when I start typing int SomeFunc() I get an error telling me that not all code paths return a value, which is great : it reminds me that I must not forget to type in a return statement somehow. In the little project I'am working on my problem is solved by making the assignment of MyFunc(); to a variable of the right type. In a Pascal-like language I believe you always have to assign a function to the value it returns(have been programming in Modula-2). I know C and C++ allow such things freely but I expected that C# would at least give me a warning here.
To Antenka:
Your hint was very helpfull.
To dickersonka:
It does not matter what the function returns. It's about the fact that you can use it without returning anything.

I hadnt checked the if (a=b) thing, I did however assume that would work like c/c++ would, my bad. :P

I fell fowl of the ignored the return value of a function when I was using the string replace function, quickly hacking some code together for something and forgot to assign the changed string back to itself..

I am surprised on its pickiest warning level it doesnt mention that you didnt use the returned value, perhaps you should suggest it for VS2010 which is in beta..

At the same time, I cant help but wonder then how many times we all use some function and ignore its return code... and how many warnings it would generate if it did actually warn us..

commented: Thanks for the help +2

:)

Thanks dickersonka!
This is surely a path I have to investigate further.
I think we can go on and on and on about this subject(I have a feelin it is in a way rather fundamental, but not of that much importance), but will mark it as solved for the moment. THanks to you all.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.