Donald Trump arrives to speak to the Conservative Party of New York Presidential Convention in New York on Sept. 7. (AP Photo/Evan Vucci)

Donald Trump said Wednesday that he is open to allowing undocumented immigrants to legally stay in the United States if they serve in the military.

"I think that when you serve in the armed forces, that's a special situation, and I could see myself working that out. Absolutely," Trump said during NBC's Commander-in-Chief Forum on Wednesday night when a member of the audience asked him whether he would allow undocumented immigrants to gain legal status by serving in the military.

"I think military is a very special thing," Trump said, expanding on his answer. "If they plan on serving, if they get in, I would absolutely, for all of those people. Now, we have to be very careful, we have to vet very carefully, everybody would agree with that, but the answer is it would be a very special circumstance, yes."

Democratic presidential nominee Hillary Clinton and her Republican opponent Donald Trump answered questions on national security and foreign policy Sept. 7 during a "commander-in-chief forum" on NBC News. (Video: NBC News/Photos: Melina Mara/Post, Mike Segar/Reuters)