Assignment in an if statement
I have a class Animal
, and its subclass Dog
.
I often find myself coding the following lines:
if (animal is Dog)
{
Dog dog = animal as Dog;
dog.Name;
...
}
For the variable Animal animal;
.
Is there some syntax that allows me to write something like:
if (Dog dog = animal as Dog)
{
dog.Name;
...
}
Solution 1:
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.
Solution 2:
If as
fails, it returns null
.
Dog dog = animal as Dog;
if (dog != null)
{
// do stuff
}
Solution 3:
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.
Solution 4:
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.