4. If the three roots of the equation $x^{3}-5 x^{2}+(4+k) x-k=0$ can be the three sides of an isosceles triangle, then the value of the real number $k$ is ( ).
(A) 3
(B) 4
(C) 5
(D) 6
4. B.

Obviously, $x=1$ is a root of the equation. Then
$$
(x-1)\left(x^{2}-4 x+k\right)=0 \text {. }
$$

According to the condition, $x=1$ is a root of $x^{2}-4 x+k=0$, or $x^{2}-4 x+k=0$ has two equal roots.
Thus, $k=3$ or 4.
When $k=3$, the three roots of the equation are $1, 1, 3$, which cannot be the three sides of an isosceles triangle;

When $k=4$, the three roots of the equation are $1, 2, 2$, which can be the three sides of an isosceles triangle.