views:

137

answers:

5

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

+3  A: 

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
`if(true)` is vaid and `if(someString == Strings.Empty)` is valid because your using the comparsion operators to validate some context. +1
RobertPitt
+3  A: 

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
+5  A: 

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
The void version doesn't work in C++ either.
Christian
The type of the condition in an `if` statement has to be implicitly convertible to `bool` in C++, too.
sbi
`if (x = 10)` will generate a warning on most recent compilers btw.
ereOn
@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
@ereOn: Hence my comment directly below the code :)
Jon Skeet
@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
@sbi: So is it just that *any* type is implicitly convertible to bool? Or just quite a few (pointer types, integers etc)?
Jon Skeet
@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
@Jon Skeet: oops! Totally missed it. My bad ;)
ereOn
A: 

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#
Henk Holterman
This is what even i experienced while working on C, C++. that's too on Turbo Compiler. For other compiler I dont know.
Shantanu Gupta
+1  A: 

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) { ... }

jdv