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# Adding More Methods

Michelle Marroquin Lopez
Michelle Marroquin Lopez
3,096 Points

Using void vs. string to print to the console

I completed this method as follows:

public void GetDisplayText()
        {
             Console.WriteLine($"Book Title: " + Title + " Book Author: " + Author);
        }

In the video, he used string as the return type and then used Console.WriteLine in the main program to print the string from the method.

Are both of these methods ok or is there a reason why the way he did it is preferred?

Thank you

1 Answer

Steven Parker
Steven Parker
229,921 Points

If you're responsible for the overall design and not being given a sub-task to implement by an employer, then the final functionality is essentially the same if you output the string inside the function or back at the caller.

However, the name "GetDisplayText" implies that the function will be returning a string (and not creating any output), so a more "best practice" name for your version might be "WriteDisplayText" or "OutputDisplayText".