Cast checking and casting in the same line

PHOTO EMBED

Wed Jul 27 2022 17:22:45 GMT+0000 (UTC)

Saved by @Marcos_

        public ICommand<T>? TryGetCommand<T>(NodeCommandKeys key) {
            if (m_commands.TryGetValue(key, out ICommand? command) && command is ICommand<T> commandT) {
                return commandT;
            }
            else {
                return null;
            }
        }
content_copyCOPY

Using the TryGetValue() dictionary retrieval method, we retrieve the command from the key then we check if the ICommand can be converted to type ICommand<T> of typw ICommand<T>, using the "is" syntax. if that evaluates to true, we then cast the ICommand to the ICommand<T> for the return statment