How to solve $y' = -2x -y$ My thought:
$\displaystyle\frac{dy}{dx}+x^0y=-2x$
Considering it as the form of linear equation, $\displaystyle\frac{dy}{dx}+P(x)y=Q(x)$
Multiplying $e^{\int1dx} = e^x$ on both sides, 
$e^x\displaystyle\frac{dy}{dx}+e^xy=-2xe^x$
$\displaystyle\frac{d}{dx}(e^xy)=-2xe^x$
$e^xy=\int-2xe^xdx$
$e^xy=-2(xe^x-e^x)$
$y=-2(x-1)+C$

It seems to me that the solution is wrong because i cannot move back to my original question from here. 
Is my answer correct? If not, can anyone tell me how to solve this problem with explanation. 
 A: $\displaystyle\frac{dy}{dx}+y=-2x$, Which is linear in $y$
$IF=e^{\int1dx} = e^x$ , Hence General Solution is given by
$e^xy=\int-2xe^xdx+c$
$e^xy=-2(xe^x-e^x)+c$
$y=-2(x-1)+ce^{-x}$
$y+2(x-1)=ce^{-x}$
A: First note $y=-2(x-1)+C$ is solution if and only if $C=0$
Now take $y(x)=-2(x-1)+k\cdot e^{-x}$ where $k$ is an arbitrary constant. We have $y'(x)=-2-k\cdot e^{-x}$ and so $y'+y=-2x$ and we have a solution of the ode different from what you have derived.
How did I get it
Obviously the function you got ($y(x)=-2(x-1)$) is a particular solution of the ode. The theory of linear ode tells us that general solution is the sum of that particular solution and a solution of the corresponding homogeneous ode namely $y'+y=0$ whose general solution is $k\cdot e^{-x}$ so the general solution of the inhomogeneous ode is $y(x)=-2(x-1)+k\cdot e^{-x}$ as expected.
What if you do not have a particular solution of the inhomogeneous linear ode
You start with the general solution of the homogeneous equation i.e $C\cdot e^{-x}$ and you let the "constant vary". Precisely you look for a solution to the inhomogeneous ode of the form $y(x)=C(x)\cdot e^{-x}$. We have $y'=C'(x)\cdot e^{-x}-C(x)\cdot e^{-x}=-y-2x$
This gives $C'(x)=-2x\cdot e^x$. Integrating by parts we get $C(x)=-2(x-1)\cdot e^x+k$ and so $y(x)=-2(x-1)+k\cdot e^{-x}$ as expected.
Now where is your mistake
You simply forgot that a primitive is defined up to a constant when you integrated the derivative of $x\cdot e^x$ and then added a constant at the end with no reason.
A: First consider the homogenous solution to the problem: $y' = -y$ with the solution $y(x) = Ce^{-x}$ for some constant $C$. Then regard $C = C(x)$ and plugging it into the equation yields: $C'(x)e^{-x} = -2xe^x$ which gives us $C(x) = \int -2xe^x dx = 2e^x(1-x)$. Finally we get $y(x) = Ae^{-x} + 2(1-x) $ for a constant A.
