I recommend you watch the Ted Talk video I posted above.
Now when you say "gain weight" I presume you are talking the guy on the right trying to get his body fat into the normal range rather than going to the gym to look like the guy on the left.
Although not universally accepted, many, many doctors in the obesity field believe in "set point". Meaning that in the short term, the body has a specific weight it wants to be. So if you gain 20 pounds in the next month, the body will start releasing hormones to reduce appetite and revving up the metabolism engines to try to burn that 20 pounds off. If you lose 20 pounds the body will lower metabolism. I forget which movie it was that I saw where they forced a group of students to overeat for a month. They were to eat THOUSANDS more calories per day than normal such that they would gain 20 pounds in just a few months. One asshat in the study gained NOTHING because he had this super metabolism. But anyhow, after the study was over and the students went back to their normal diets, the majority of them went right back to their normal weight within a couple months. There is that pull back to the set point.
HOWEVER, studies have shown that the set point CAN be moved up. As the Ted Talk video says, it does take time - usually about a year - of maintaining the higher weight - and the set point will move up. But rarely, if ever, down. She cites 7 years, but that's because that's the longest study that has been done. So that thin-as-a-rail guy who can't seem to gain weight... if he can sustain a 10 pound gain for a year, the body will adjust to that higher weight and think that is its new normal. But someone that loses 10 pounds.... noop ... sorry. You are out of luck.
Now I purchased Dr. Sharma's workbook when I was in Calgary last July and it is his belief that the further you pull away from your set point, the harder it is to maintain that weight. The further you force your weight to be away from the set point, the more the body will rebel against you.... modifying satiety, playing with neuroreceptors and hormone levels to increase/lower hunger, make you think about food constantly or not at all, and of course playing with your metabolism. I spoke with him after his speech and he said it's like an elastic band. The more you pull away from your set point, the more the body wants to pull you back to that set point. And like I said, in the short term, that works in BOTH directions. But in the long term, our biology is built so that thin people can become accustomed to being a higher weight, but overweight people do not become accustomed to being the lower weight. They have to continue fighting forever.
I actually asked Dr. Sharma outright just if I really had to fight this FOREVER. His response was, "No. Not forever. Only until you die. Which likely will be many more years into the future than if you had not lost the weight and continue to keep it off."