4. The graph of a certain linear function is parallel to the line $y=\frac{1}{2} x+3$, and intersects the $x$-axis and $y$-axis at points $A$ and $B$, respectively, and passes through point $C(-2,-4)$. Then, on the line segment $AB$ (including points $A$ and $B$), the number of points with both integer coordinates is ( ) .
(A) 3
(B) 4
(C) 5
(D) 6
4. B.

According to the problem, let the analytical expression of the linear function be
$$
y=\frac{1}{2} x+b \text {. }
$$

Since point $C(-2,-4)$ lies on the graph of this function, we have
$$
-4=\frac{1}{2} \times(-2)+b \Rightarrow b=-3 \text {. }
$$

Therefore, $y=\frac{1}{2} x-3$.
We can find points $A(6,0)$ and $B(0,-3)$.
Given $0 \leqslant x \leqslant 6$, and $x$ is an integer, when $x=0,2,4,6$, the corresponding $y$ values are integers.

Thus, on line segment $A B$ (including points $A$ and $B$), there are 4 points where both the x-coordinate and y-coordinate are integers.