Viewers of The Good Wife were gobsmacked by the sudden, unexpected death of its dashing attorney, Will Gardner, on Sunday's episode of the CBS legal drama.

Gardner, a prominent character portrayed by Josh Charles since the series' debut five seasons ago, was gunned down in a Chicago courtroom by his unhinged client. He was pronounced dead shortly afterward.

Once the episode had aired, Twitter lit up with astonished, sorrowful and even irate posts.

One viewer called for a group hug, while another issued a plea for a counsellor to treat jolted fans. Yet another viewer expressed hope that, as with Dallas long ago in revealing Bobby Ewing's death to be only a dream, The Good Wife would find Gardner stepping out of his shower next week, alive and well.

Other viewers vowed never to watch the show again, as payback. Charles said he appreciated the support of fans, but urged those who are upset to take a step back.

"I understand that there will be audience members who will be upset," he said. "I would just tell them that I guarantee it will continue to be an excellent show. I know I'm going to keep watching it."

The Good Wife

Julianna Margulies stars as lawyer Alicia Florrick in The Good Wife, a series about high ambition and shifting alliances in a grown-up world of politics and law. (David Giesbrecht/Associated Press)

The decision was driven by Charles' desire to leave the drama. He had told the show's producers that "I was ready for the next chapter of my life, both creatively and personally," in an interview excerpt provided by CBS on Monday.

Gardner portrayed a rival lawyer and former lover of the character played by series star Julianna Margulies. The twist will force her character to examine all aspects of her life, producers said.

"She's going to miss the existence of Will," Margulies said of her character. "He was a powerful aphrodisiac in her life."

The death and Charles' decision to exit the show had been well-guarded secrets. Charles, who entered the series with a four-year contract, opted not to re-up when it expired after last season, according to his publicist.

Charles' past TV work includes the ABC dramedy series SportsNight and a season playing a client of therapist Gabriel Byrne on the HBO drama In Treatment.