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 (Retired) Console I/O Start Coding

Ok, why is writing screen system.console.write()?

I'm trying to learn C#. Before starting this course, I knew some basics about C#. (by far not an expert.) But everywhere else I've been working the language, the write out to the screen command is Console.WriteLine("Test"); Team Treehouse, the command is System.Console.Write("Test"); why is it different than everything else? Or am I just way off base? (I tried putting in team treehouse editor my way and it gives a compiler error... so teamtreehouse totally doesn't recognize it as correct... I guess I would like to know the world way of doing this stuff, not team treehouse where it works in their compiler but nothing else.)

3 Answers

andren
andren
28,558 Points

Console is a class that belongs to the System namespace. Namespaces are very simply put collections of stuff in C#. Write and WriteLine are both methods belonging to the Console class. The only difference between the two methods is that WriteLine automatically places a line break after printing, which means that if you print multiple things it will appear on different lines, which is not the case with Write.

To access the stuff stored in a namespace you can either type out the full name whenever you want to use something from it. Which is what is going on with the System.Console.Write("Test") command. Or you can tell C# that you intend to use things stored in a specific namespace by using a command called using.

If you use the using command then C# will allow you to access stuff from that namespace without actually typing the name of the namespace itself. Most C# projects have a using System; line placed at the top automatically by the IDE that generates the project. Which is why you can usually use the Console.Write method without specifying the System namespace.

If you added using System; at the very top of your Treehouse program then you would be able to use it without referencing System on Treehouse as well.

Edit: Added extra info on Write and WriteLine.

Steven Parker
Steven Parker
231,275 Points

There's different methods for different purposes.

The "WriteLine" method puts out the string argument and then a "newline" sequence, so the next output will be on a different line. The "Write" method only puts out your string, so unless the string itself contains the characters for going to the next line, the next output will go to the same line. So except for that line behavior they do the same thing. You'll see both of them used in the courses here.

Now the "System" prefix is the namespace of the "Console" class. You don't need it if you have declared that your code relies on that namespace. In that case, you'll have a line like this, often at the top of the source file:

using System;

With that, you can use "Console.WriteLine" or "Console.Write" without putting the "System." in front. This is something also done both ways in the courses here, and one of them will talk specifically about the using statement.

Ok, makes sense... like I said, I'm at the elementary stages. I've been trying to learn Powershell also, and the explanation you give me about write vs writeline kind of sounds familiar. I don't remember exactly what the differences are, but that's probably it...