0

I don't understand this while loop structure. How does the ductCable get assigned? I see the logic test saying: "if type is IMMDuctCabe do stuff". But it looks like it is assigning that value at the same time.

items.Reset();
var item = items.Next();
while (item != null)
{
    if (item is IMMDuctCable ductCable)
    {
        var newDuctCable = new MMDuctCableClass();
        newDuctCable.cableID = ductCable.cableID; 
        ....
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
fallingdog
  • 192
  • 1
  • 2
  • 17
  • Read about the Type Pattern: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/is#type-pattern – madreflection Apr 23 '21 at 17:57
  • 3
    The statement is equivalent to `if (item is IMMDuctCable) { var ductCable = item as IMMDuctCable; /* rest of if bloc */ `. It's easier to type and to read - it's usually best to let the compiler do the work for you – Flydog57 Apr 23 '21 at 17:58
  • If you want "if type is IMMDuctCabe do stuff" logic then the code should be `if (item is IMMDuctCable) {..}`. In your case you additionally provide a variable to fill with the value. – AgentFire Apr 23 '21 at 18:01

0 Answers0