Yes you can put assignment operator (=) inside if conditional statement(C/C++) and its boolean type will be always evaluated to true since it will generate side effect to variables inside in it.
The line: if(b = ! b) first reverses (or technically "negates") the value of b and then "assigns" it to b. It is good to know that assignment operation in Java returns the object to which value is being assigned. So, the value of b is returned as the condition for if-statement.
It means you try to assign a value whereas,in the same time, you try to compare these values. To compares two values it's '==' or '==='. To assign a value to a variable it's '=' Submitted by Clarisse.
No. Assignment in Python is a statement, not an expression.
The answer below was written years ago and updated over time. As of C# 7, you can use pattern matching:
if (animal is Dog dog)
{
// Use dog here
}
Note that dog
is still in scope after the if
statement, but isn't definitely assigned.
No, there isn't. It's more idiomatic to write this though:
Dog dog = animal as Dog;
if (dog != null)
{
// Use dog
}
Given that "as followed by if" is almost always used this way, it might make more sense for there to be an operator which performs both parts in one go. This isn't currently in C# 6, but may be part of C# 7, if the pattern matching proposal is implemented.
The problem is that you can't declare a variable in the condition part of an if
statement1. The closest approach I can think of is this:
// EVIL EVIL EVIL. DO NOT USE.
for (Dog dog = animal as Dog; dog != null; dog = null)
{
...
}
That's just nasty... (I've just tried it, and it does work. But please, please don't do this. Oh, and you can declare dog
using var
of course.)
Of course you could write an extension method:
public static void AsIf<T>(this object value, Action<T> action) where T : class
{
T t = value as T;
if (t != null)
{
action(t);
}
}
Then call it with:
animal.AsIf<Dog>(dog => {
// Use dog in here
});
Alternatively, you could combine the two:
public static void AsIf<T>(this object value, Action<T> action) where T : class
{
// EVIL EVIL EVIL
for (var t = value as T; t != null; t = null)
{
action(t);
}
}
You can also use an extension method without a lambda expression in a cleaner way than the for loop:
public static IEnumerable<T> AsOrEmpty(this object value)
{
T t = value as T;
if (t != null)
{
yield return t;
}
}
Then:
foreach (Dog dog in animal.AsOrEmpty<Dog>())
{
// use dog
}
1 You can assign values in if
statements, although I rarely do so. That's not the same as declaring variables though. It's not terribly unusual for me to do it in a while
though when reading streams of data. For example:
string line;
while ((line = reader.ReadLine()) != null)
{
...
}
These days I normally prefer to use a wrapper which lets me use foreach (string line in ...)
but I view the above as a pretty idiomatic pattern. It's usually not nice to have side-effects within a condition, but the alternatives usually involve code duplication, and when you know this pattern it's easy to get right.
If as
fails, it returns null
.
Dog dog = animal as Dog;
if (dog != null)
{
// do stuff
}
You can assign the value to the variable, as long as the variable already exists. You can also scope the variable to allow that variable name to be used again later in the same method, if that is a problem.
public void Test()
{
var animals = new Animal[] { new Dog(), new Duck() };
foreach (var animal in animals)
{
{ // <-- scopes the existence of critter to this block
Dog critter;
if (null != (critter = animal as Dog))
{
critter.Name = "Scopey";
// ...
}
}
{
Duck critter;
if (null != (critter = animal as Duck))
{
critter.Fly();
// ...
}
}
}
}
assuming
public class Animal
{
}
public class Dog : Animal
{
private string _name;
public string Name
{
get { return _name; }
set
{
_name = value;
Console.WriteLine("Name is now " + _name);
}
}
}
public class Duck : Animal
{
public void Fly()
{
Console.WriteLine("Flying");
}
}
gets output:
Name is now Scopey
Flying
The pattern of variable assignment in the test is also used when reading byte blocks from streams, for example:
int bytesRead = 0;
while ((bytesRead = fs.Read(buffer, 0, buffer.Length)) > 0)
{
// ...
}
The pattern of variable scoping used above, however, is not a particularly common code pattern and if I saw it being used all over the place I'd be looking for a way to refactor it out.
Is there some syntax that allows me to write something like:
if (Dog dog = animal as Dog) { ... dog ... }
?
There likely will be in C# 6.0. This feature is called "declaration expressions". See
https://roslyn.codeplex.com/discussions/565640
for details.
The proposed syntax is:
if ((var i = o as int?) != null) { … i … }
else if ((var s = o as string) != null) { … s … }
else if ...
More generally, the proposed feature is that a local variable declaration may be used as an expression. This if
syntax is just a nice consequence of the more general feature.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With