Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

C# C# Basics (Retired) Perfect Doubles

Knud Back
Knud Back
1,248 Points

Help 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
Steven Parker
220,696 Points

The 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
Knud Back
1,248 Points

Thank 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