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

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

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -