2

Hi we can not define in c# variable like this

if((var input = db.table.FirstOrDefault()) != null)
{
   // and here I could use my 'input' variable
}

but i can do it this way

for(var input = db.table.FirstOrDefault(); input != null; input = null)
{
  //opeartion

}

Can anyone tell me why? Woudn't that be nice if we could do it using if ?

Peter
  • 13,733
  • 11
  • 75
  • 122
kosnkov
  • 5,609
  • 13
  • 66
  • 107
  • Why on earth would you want to do that? `for()` is used for looping, these two snippets of code are completly different – Shai Aug 02 '12 at 13:38
  • @Shai I won't, i wish to be able to use first solution, second is only to show that this is possible in short way. – kosnkov Aug 02 '12 at 13:39
  • You can't do it because C# doesn't let you. *Why* C# doesn't let you is a *subjective* questionthat only the people who designed C# can answer (but most likely is because "its confusing and not all that useful") – Michael Edenfield Aug 02 '12 at 13:41

4 Answers4

2

Because that leads to long-known hard-to-track errors:

if (var t = true) {}

bool t;
if (t = true) {}

bool t;
if (t == true) {}
Sergei Rogovtcev
  • 5,804
  • 2
  • 22
  • 35
2

As to the why, it's because input = db.table.FirstOrDefault() is a statement and doesn't actually return anything. It assigns something to the input variable. As it doesn't return anything (i.e. it's a statement), you cannot compare it to something else.

The if expects an expression (something that returns something). Here's more on the difference between statements and expressions.

This is different from C where (if I'm not mistaken) everything that has a value of 0 if false and everything else is true.

As to the closest you can get with C#, I believe you can do something like:

MyClass input = null;
if ((input = db.table.FirstOrDefault()) != null)
{
    // use input here
}

But then you might as well do:

MyClass input = db.table.FirstOrDefault();
if (input != null)
{
    // use input here
}
Community
  • 1
  • 1
Peter
  • 13,733
  • 11
  • 75
  • 122
  • off course i know i can do it this way, I was just wondering if this idea is worth to think to implement. – kosnkov Aug 02 '12 at 13:55
  • Just being complete (hey, other, less experienced developers might land on this page). The question (the why) is answered in the first part of my answer (difference between statements and expressions in C#). – Peter Aug 02 '12 at 14:00
2

This is because writing

object input = db.table.FirstOrDefault();

is exactly the same like writing

object input; 
input = db.table.FirstOrDefault();

accoring to the C# specification.

This means that there is no any return value, so there is nothing to check !=null against.

Yes, I think it's possible to trick this, to make some changes in compiler, but probabbly it doesn't worth effort.

Tigran
  • 61,654
  • 8
  • 86
  • 123
0

In a for loop you define index variables for iteration. You could write it like that:

var i = 0;
for (; i < x; ++i)
{
    // Some code;
}

But in that case the i variable would exist outside of the iteration, so for loop creates variable with a scope of the iteration. In an if you don't need additional variables for it to work.

aikixd
  • 506
  • 5
  • 16