Understanding the derivation of the formula for change in internal energy of a gas in an enclosed cylinder 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?
 A: 
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?

No, it's not correct. The change in internal energy for an ideal gas, for ANY process, is given by
$$ dU = nC_{v}dT$$
This is a consequence of the ideal gas law and the unique relationship between the specific heats for an ideal gas.
For example, let's consider a constant pressure process.
$$dU=dQ-pdV$$
$$dU=nC_{p}dT – PdV$$
From the ideal gas equation, constant pressure process
$$PdV=nRdT$$
Therefore
$$ dU=nC_{p}d T – nRdT$$
For an ideal gas
$$R=C_p-C_v$$
$$dU=nC_pdT – n(C_p-C_v)dT$$
$$dU=nC_{v}dT$$
Let's consider an adiabatic process. From first law ($dQ=0$)
$$dU=-dW=-pdV$$
$$dU=- \frac {nRdT}{1-k}$$
For an ideal gas
$$k=\frac{C_p}{C_v}$$
and again
$$R=C_p-C_v$$
Therefore
$$dU=- \frac{n(C_p-C_v)dT}{1-C_p/C_v}$$
$$dU= nC_vdT$$
Hope this helps.
A: For a arbitrary isotropic fluid, the combination of the 1st and 2nd laws of thermodynamics tells us that $$dU=TdS-PdV$$From this relationship, the following equation can be derived:  $$dU=C_vdT+\left[P-T\left(\frac{\partial P}{\partial T}\right)_V\right]dV$$For an ideal gas, the term in brackets is zero.
