In a new biography, actor Sir Ian McKellen discusses his decision to come out gay at 49.

The 80-year-old McKellen, who first spoke publicly about his sexuality during an interview with the BBC in 1988, is best known for playing Gandalf in The Lord of the Rings and The Hobbit trilogies and Magneto in the X-Men films.

In the upcoming biography Ian McKellen: A Biography by Garry O'Connor from St. Martin's Press, McKellen said that he came out to become an advocate for the LGBT community.

“I didn't really know who I was – a closeted gay,” he said.

McKellen met O'Connor while they were studying at Cambridge University in 1958. O'Connor has since directed the actor in various plays.

“Cambridge was great for me … all the many parts I had played there, as I loved for the first time going out in public and displaying my emotions. I enjoyed disguising myself as a closeted gay boy,” he said.

McKellen said that while he was out to close friends and colleagues, he worried how being open about his sexuality would affect his career. Additionally, gay sex was a crime in England until 1967, though certain conditions not imposed on heterosexual couples remained. It wasn't until 2003 that the age of consent was lowered from 21 to 16 for gay men and other conditions related to privacy were removed.

After coming out, McKellen said that his career flourished as he became an outspoken advocate for LGBT rights.

“He had left the dark closet of secrecy for self-exposure and liberation,” O'Connor wrote. “This galvanized him, energized him in a direction he had never thought himself capable of.”

McKellen currently appears opposite Helen Mirren in The Good Liar.