Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

C# C# Basics Methods Variable Scope

How did method name MyMethod() got executed ?

How did method name MyMethod() got executed even though it was not called in Main method and compiler threw an error ? since it was not called shouldn't that method MyMethod() was supposed to be ignored ?

2 Answers

Steven Vallarsa
Steven Vallarsa
10,842 Points

The MyMethod() method didn't get called. But the compiler checks everything anyway, even if it isn't going to be used, and throws the appropriate errors.

Rajat Kadia
Rajat Kadia
604 Points

In real world you can't be sure that someone else won't call that method at runtime using reflection. Your code MUST compile or it's not valid code - if it's never used... comment it out! Hopefully this answers your question.