The derivation:
Let, there is $m$ mole of gas, $p$ amount of pressure, $v$ amount of volume, $T$ amount of temperature, and $U$ amount of internal energy. Now, $dQ$ amount of heat is supplied to this gas so that its internal energy changes by $dU$ and external work done by the gas be $dW$. Also, if the volume of the cylinder is increased by $dV$ amount, work done or $dW = pdV$ So, from the 1st law of thermodynamics we get,
$$dQ = dU + dW$$ $$\implies dQ = dU + pdV$$
If volume remains constant, $dV = 0$. So, the equation becomes,
$$dQ = dU...(i)$$
Now, we know that keeping the volume constant, if $dQ$ amount of heat is applied to $m$ mole of gas to increase its temperature by $dT$, then the molar specific heat at constant volume, $C_v$, is
$$C_v = \frac{dQ}{mdT}$$
$$dQ = mC_vdT$$
Putting the value of $dQ$ from $eq^n (i)$,
$$dU = mC_vdT$$
"As the change in internal energy of an ideal gas only depends on the change in temperature and the number of moles, we can use the above equation anytime when the temperature of $m$ mole of gas changes by $dT$; its not necessary for the volume of gas to remain constant for us to use this equation" $-$ this is what my book says and this is the part I don't get.
We derived the equation considering the volume constant or $dV=0$, so we can't use this equation when the volume is changing. Am I incorrect?