Idiomatic loop and break condition I am calling a C library via P/Invoke. That library needs to be called in sucession with an increasing unsigned int, and returns a pointer. That pointer is then mapped to a managed type, until it returns a NULL pointer. 

In C, the idiomatic way to write it, is probably for(i=0;;i++), but what is the most idiomatic way to write it in C#?

Currently it is using a do {} while loop, as in my opinion, this is the clearest way to show that this loop will repeat until newPort is null.

static IEnumerable&lt;Port&gt; GetPorts ()
{       
    List&lt;Port&gt; ports = new List&lt;Port&gt;();
    uint i = 0;
    Port newPort;
    do {
        // This calls an C library, and maps the returned pointer
        // to the Port class
        newPort = GetPortData (i);
        if (newPort != null){
            ports.Add (newPort);
        }
        i++;
    } while(newPort != null);
    return ports;
}


On the other hand, the variables i and newPort are only used inside the loop, so using for would be another solution, but it does not clearly show the breaking condition.

    for (uint i = 0; ; i++) {
        Port newPort = GetPortData (i);
        if (newPort == null) {
            break;
        }
        ports.Add (newPort);
    }


Which version should I use?
I think the for loop can show the "breaking condition" like this:

for (uint i = 0; (newPort = GetPortData(i)) != null; i++) {
    ports.Add (newPort);
}