c# - Changing console color, gives me a Stackoverflow Exception -


i use following methods printing text in different color on console.

        private static void writeupdatedbookingdetails(string text)         {             console.foregroundcolor = consolecolor.darkgreen;             writeupdatedbookingdetails(text);         } 

when execute writeupdatedbookingdetails() method following code, gives me exception

an unhandled exception of type 'system.stackoverflowexception' occurred in mscorlib.dll)

        static void main(string[] args)         {             ...             // exception occurred when call method.              writeupdatedbookingdetails("\n- - reconciling updated bookings - -");              ...             }         } 

your problem used recursion. when call method, foreground first set dark green. can see here, call same method again! forms infinite loop!

when loop loops lot of times, stack overflows. that's why stackoverflowexception occurs. guess want call

console.writeline (text); 

so how method should like:

private static void writeupdatedbookingdetails(string text) {     console.foregroundcolor = consolecolor.darkgreen;     console.writeline(text); } 

then, method not call itself, no more recursion!


Comments

Popular posts from this blog

java - WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/board/] in DispatcherServlet with name 'appServlet' -

html - Outlook 2010 Anchor (url/address/link) -

android - How to create dynamically Fragment pager adapter -