Building a string with only two repeating characters
Your algorithm has a flaw - it's too greedy. Take this example:
Foo(6, 2) // &quot;AABBAAAA&quot;

Alas, it should return &quot;AABAABAA&quot;.
In terms of a review, your method shouldn't be called Foo - give it a descriptive name.
Great use of StringBuilder. You could initialize the length to a+b in the constructor as you know what size your finished string will be.
Expression bodied members can greatly improve readability in my opinion:
static char SwapChar(char c) =&gt; 
    c == 'A' ? 'B' : 'A';

Try to avoid variables like flag. I had to read the whole loop before I knew what that variable meant. I'd call it something like repeatCount.