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 trialKnud Back
1,248 PointsHelp understanding the fundamentals of C# syntax
It seems like there is two different ways to make method calls in C#. By different I mean the order of namespace class and method. Is there a general rule regarding when to use each "style".
Example: string my_variable
The first way System.Console.Writeline(my_variable) This call follows the order namespace.class.method(variable)
The second way my_variable.ToLower() This call follows the order variable.method
I am not sure how to Google this question. Therefore I would appreciate any help
1 Answer
Steven Parker
231,269 PointsThe difference is class method vs. instance method.
Your first example is a class (or "static") method which may have up to the 3 parts you mentioned. It might not need them all, depending on where the call is being made and what using statements have been done, but will always have the method name.
The second example is of an instance method, which is called on a specific instance of a class represented by a variable. The namespace and class are determined by the variable, so only the method name is used. In your example they are System.String.
Knud Back
1,248 PointsKnud Back
1,248 PointsThank You, for your answer. Your explanation was a great help. Based on your answer I found this video on Youtube which might be useful for anyone reading this tread. https://www.youtube.com/watch?v=bfRqK6zjVuk