2. In quadrilateral $ABCD$, $\angle A=\angle C=90^{\circ}$, $AB=AD$, $AE \perp BC$ at $E$. If the area of quadrilateral $ABCD$ is the maximum value of the binary function
$$
y=-x^{2}-2 k x-3 k^{2}-4 k+16
$$

then the length of $AE$ is ( ).
(A) 3
(B) $\sqrt{21}$
(C) $3 \sqrt{2}$
(D) Cannot be determined
2.C.

As shown in Figure 1, draw $A F \perp C D$, with $F$ as the foot of the perpendicular.

In quadrilateral $A B C D$, since
$$
\begin{array}{l}
\angle B A D=\angle C \\
=90^{\circ},
\end{array}
$$

thus, $\angle B$
$$
\begin{array}{l}
=180^{\circ}-\angle A D C \\
=\angle A D F .
\end{array}
$$

It is easy to see that $\triangle A B E \cong \triangle A D F$.
Therefore, $S_{\triangle A B E}=S_{\triangle A D F}, A E=A F$.
Thus, quadrilateral $A E C F$ is a square, and
$$
\begin{array}{l}
S_{\text {quadrilateral } A B C D}=S_{\text {square } A E C F}=A E^{2} . \\
\text { From } y=-x^{2}-2 k x-3 k^{2}-4 k+16 \\
=-(x+k)^{2}-2(k+1)^{2}+18
\end{array}
$$

we know that when $k=-1, x=1$, the maximum value of $y$ is 18.
Thus, $A E^{2}=18, A E=3 \sqrt{2}$.