jeudi 26 août 2021

Why does C# ask me to put a semicolon after the else statement? [closed]

public class Solution {
    class Trie {
        public char c;
        public IDictionary<char, Trie> next = new Dictionary<char, Trie>();
        public bool emailEndsHere = false;
    }
    
    public int NumUniqueEmails(string[] emails) {
        int res = 0;
        
        var root = new Trie();
        foreach(var email in emails) {
            if(add(email, root)) {
                ++res;
            }
        }
            
        return res;
    }
    
    static bool add(string email, Trie root) {
        var curr = root;
        void makeStep(char c) {
            if(curr.next.ContainsKey(c)) {
                curr = curr.next[c];
            } else {
                var node = new Trie { c = c };
                curr.next[c] = node;
                curr = node;
            }
        }
        
        bool inDomain = false;
        bool ignoreUntilDomain = false;
        foreach(var c in email) {
            if(c == "@") {
                inDomain = true;
                makeStep(c);
            } else if(c == ".") {
                if(inDomain) {
                    makeStep(c);
                }
            } else if(c == "+") {
                if(inDomain) {
                    makeStep(c);
                } else {
                    ignoreUntilDomain = true;
                }
            } else (inDomain || !ignoreUntilDomain) {
                makeStep(c);
            }
        }
        
        var res = !curr.emailEndsHere;
        curr.emailEndsHere = true;
        
        return res;
    }
}

While trying to execute the code above I am getting the following error message:

Line 49: Char 53: error CS1002: ; expected (in Solution.cs)

For some reasons C# wants me to put a semicolon in this line : } else (inDomain || !ignoreUntilDomain) { and I can not figure out why.

Could someone help me with this problem, please?

I am typing this code here if it matters.

Aucun commentaire:

Enregistrer un commentaire