-2

What will be the output if I write

In C++ if(5) will be executed without any problem but not in C# same way will it be able to run.

if(func()){} //in C# it doesn't runs Why how does C# treats void and how in Turbo C++

void func()
{
return;
}

if(null==null){}//runs in C#

EDIT

if(printf("Hi"){} //will run and enter into if statement

if(printf(""){}//will enter into else condition if found.

This Question is not meant for those who are not aware of Turbo Compiler

Shantanu Gupta
  • 20,688
  • 54
  • 182
  • 286
  • are you saying that `if(void)` works in Turbo C++ ? – H H Jul 15 '10 at 11:36
  • That's an error in C++ as well. And the question isn't whether it __runs__, but whether it __compiles__. Both C++ and C# are _compiled_ languages, not _interpreted_ ones. So that question makes no sense at all. `-1` from me. – sbi Jul 15 '10 at 11:47
  • @sbi: No issues with your -1, thanks anyway. I am not considering VS C++, I specified explicitly for turbo compiler. @Henk: Yes I do mean the same but only in Turbo Compiler – Shantanu Gupta Jul 15 '10 at 11:51
  • @Shantanu: Sorry, but I simply don't believe that any version of TC ever allowed you to compile `if(func())` if `func()` returns `void`. – sbi Jul 15 '10 at 12:04
  • @sbi: This is allowed in Turbo compiler. This compiler was used in 1990's and I too have worked on it till 2008 and never worked on VC++ and also I have seen most of unexpected behaviour in this compiler when executing any increment operators. – Shantanu Gupta Jul 15 '10 at 12:26
  • 1
    @Shantanu: You might want to change the text of your question then: "When I'm working with a broken C++ compiler..." – Jon Skeet Jul 15 '10 at 12:57
  • @Shantanu: Even if you don't want to switch to **VC++**, you still can try **g++**. It is probably far less broken than your actual compiler. – ereOn Jul 15 '10 at 13:26
  • @Jon: Nice Comment, i like it. @jon & @ereOn: Now I works on C# just know details about the different behaviour I asked this. Thanks to all anyway – Shantanu Gupta Jul 15 '10 at 13:45

5 Answers5

5

In C# the type of the condition in an if statement has to be implicitly convertible to bool. This reduces errors in various situations, and is basically a Good Thing. It prevents things like this from compiling:

int x = ...;
if (x = 10) // Valid in C / C++, not in C#

Even in C and C++, a decent compiler will warn you on the above line though, if you have a sensible level of warnings.

I'm surprised if the void version works in C++ though...

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 3
    The type of the condition in an `if` statement has to be implicitly convertible to `bool` in C++, too. – sbi Jul 15 '10 at 11:45
  • `if (x = 10)` will generate a warning on most recent compilers btw. – ereOn Jul 15 '10 at 11:48
  • @Jon,@Christian: void works in turbo compiler. After working on turbo compiler at the time of my schooling it seems to be a loosely typed language I think – Shantanu Gupta Jul 15 '10 at 11:54
  • 1
    @Shantanu: No, it is not a loosely typed language. It is a broken C++ compiler. It is not a separate language (C++ is C++, regardless of compiler), it's just a buggy, obsolete compiler which no one should use. – jalf Jul 15 '10 at 12:54
  • @sbi: So is it just that *any* type is implicitly convertible to bool? Or just quite a few (pointer types, integers etc)? – Jon Skeet Jul 15 '10 at 12:56
  • @Jon: `struct mine {};` isn't implicitly convertible to `bool`, which is why `if(mine())` would fail to compile. Off the top of my head I can't think of any of the built-in types in C++ that are _not_ implicitly convertible to `bool`. User-defined types are if they have an implicit conversion operator either to `bool` or any of the built-ins (all?) that are convertible to `bool`. – sbi Jul 15 '10 at 13:20
  • @Jon Skeet: oops! Totally missed it. My bad ;) – ereOn Jul 15 '10 at 13:23
3

Unlike C/C++, C# conditions can only be applied to Boolean values.
Note that void function does not have return value so no condition can be applied to it.

Itay Karo
  • 17,924
  • 4
  • 40
  • 58
  • `if(true)` is vaid and `if(someString == Strings.Empty)` is valid because your using the comparsion operators to validate some context. +1 – RobertPitt Jul 15 '10 at 11:35
3

A void function does not return anything at all, thus its return value can not be checked with an if statement.

Even C++ wont let you do that.

Timbo
  • 27,472
  • 11
  • 50
  • 75
1

in C/C++, a nonzero integer value is the same as logical true. The reason for this is that C/C++ did not have a boolean type defined, so integers were used as boolean variables. Later, people found out that this kind of implicit type conversion can cause unexpected behavior when the compiler tries to find the appropriate overloaded version of the function, therefore the mistake was not repeated in C#.

To get the same behavior in C#, write if (x!=0) { ... }

0

In C and C++ there is an implicit conversion of int , pointers and most other types to bool.

The designers of C# elected not to have that, for clarity.

So with

int i = 1;
int* P = null;

if (i && p) { } // OK in C++

if (i != 0 && p != null) { }  // OK in C++ and C#
H H
  • 263,252
  • 30
  • 330
  • 514