Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The answer below was written years ago and updated over time. As of C# 7, you can use pattern matching:</p> <pre><code>if (animal is Dog dog) { // Use dog here } </code></pre> <p>Note that <code>dog</code> is still in scope after the <code>if</code> statement, but isn't definitely assigned.</p> <hr> <p>No, there isn't. It's more idiomatic to write this though:</p> <pre><code>Dog dog = animal as Dog; if (dog != null) { // Use dog } </code></pre> <p>Given that "as followed by if" is almost <em>always</em> 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 <a href="https://github.com/dotnet/roslyn/issues/206" rel="noreferrer">pattern matching proposal</a> is implemented.</p> <p>The problem is that you can't <em>declare</em> a variable in the condition part of an <code>if</code> statement<sup>1</sup>. The closest approach I can think of is this:</p> <pre><code>// EVIL EVIL EVIL. DO NOT USE. for (Dog dog = animal as Dog; dog != null; dog = null) { ... } </code></pre> <p>That's just <em>nasty</em>... (I've just tried it, and it does work. But please, please don't do this. Oh, and you can declare <code>dog</code> using <code>var</code> of course.)</p> <p>Of course you could write an extension method:</p> <pre><code>public static void AsIf&lt;T&gt;(this object value, Action&lt;T&gt; action) where T : class { T t = value as T; if (t != null) { action(t); } } </code></pre> <p>Then call it with:</p> <pre><code>animal.AsIf&lt;Dog&gt;(dog =&gt; { // Use dog in here }); </code></pre> <p>Alternatively, you could combine the two:</p> <pre><code>public static void AsIf&lt;T&gt;(this object value, Action&lt;T&gt; action) where T : class { // EVIL EVIL EVIL for (var t = value as T; t != null; t = null) { action(t); } } </code></pre> <p>You can also use an extension method without a lambda expression in a cleaner way than the for loop:</p> <pre><code>public static IEnumerable&lt;T&gt; AsOrEmpty(this object value) { T t = value as T; if (t != null) { yield return t; } } </code></pre> <p>Then:</p> <pre><code>foreach (Dog dog in animal.AsOrEmpty&lt;Dog&gt;()) { // use dog } </code></pre> <hr> <p><sup>1</sup> You can <em>assign</em> values in <code>if</code> statements, although I rarely do so. That's not the same as declaring variables though. It's not <em>terribly</em> unusual for me to do it in a <code>while</code> though when reading streams of data. For example:</p> <pre><code>string line; while ((line = reader.ReadLine()) != null) { ... } </code></pre> <p>These days I normally prefer to use a wrapper which lets me use <code>foreach (string line in ...)</code> but I view the above as a pretty idiomatic pattern. It's <em>usually</em> 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.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload