Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Start a free Courses trial
to watch this video
Namespaces are groupings of similar classes. We don't want to go into a lot of detail about namespaces in this introductory course, but almost every C# program uses them, so in this video, we're going to show you the bare minimum you need to know.
Resources
- Unit Testing in C# – Treehouse course
- Unit testing basics – Visual Studio Docs
Here's a program that picks a random floating-point number between 0 and 1, rounds it to a single decimal place, and prints it.
class Program
{
static void Main(string[] args)
{
// Create a new random number generator.
System.Random generator = new System.Random();
// Generate a floating-point number between 0 and 1.
double randomNumber = generator.NextDouble();
// Round the number to a single decimal place.
double roundedNumber = System.Math.Round(randomNumber, 1);
// Print the rounded number.
System.Console.WriteLine(roundedNumber);
}
}
Don't worry too much about the details of how this program works. But notice that like the programs we've already shown you, many of the types in this program begin with System
, followed by a dot. System
is used so frequently that writing it over and over starts to make the code look cluttered.
We write System
so frequently because we're using types from the System
namespace. Namespaces are groupings of similar classes. We don't want to go into a lot of detail about namespaces in this introductory course, but almost every C# program uses them, so in this video, we're going to show you the bare minimum you need to know.
Here's another program, a unit test. We can run it from the command line with dotnet test
.
[Microsoft.VisualStudio.TestTools.UnitTesting.TestClass]
public class UnitTestEquality
{
[Microsoft.VisualStudio.TestTools.UnitTesting.TestMethod]
public void TestEquality()
{
Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(2, 2);
Microsoft.VisualStudio.TestTools.UnitTesting.Assert.AreEqual(5, 5);
}
}
Again, don't worry too much about the details of how this works. We'll have a link in the teacher's notes if you want to learn more about unit testing [TODO]. What we want you to notice right now is that Microsoft.VisualStudio.TestTools.UnitTesting
is repeated several times throughout the code. It's another namespace.
Because writing the name of a namespace over and over can clutter your code, C# offers another shortcut. The using
directive lets you access types in a namespace without having to prepend the namespace onto the type name.
- Let me go back to the first program I showed you.
- At the top of the file, I'll type the keyword
using
, a space, and the name of the namespace I want to use, which isSystem
. And as always, each statement in a C# program needs to end with a semicolon:using System;
- That's how you write a
using
directive. - Now, if I save my work, I can still run the program without making any further changes:
dotnet run
- But now that I have the
using System
directive at the top of my file, I don't have to writeSystem
and a dot before types in theSystem
namespace anymore.- I can go through the program, and remove
System
and the dot each place it occurs. - If I save my work and run the program, you can see everything works just as it did before:
dotnet run
- I can go through the program, and remove
using System;
class Program
{
static void Main(string[] args)
{
// Create a new random number generator.
Random generator = new Random();
// Generate a floating-point number between 0 and 1.
double randomNumber = generator.NextDouble();
// Round the number to a single decimal place.
double roundedNumber = Math.Round(randomNumber, 1);
// Print the rounded number.
Console.WriteLine(roundedNumber);
}
}
If you add a using
directive, you don't have to remove the namespace from the front of type names. But you can.
Now let me show you what happens if I remove the using
directive again.
- I'll go to the top of the file, and remove the directive...
- I'll save my work, and try running the program again:
dotnet run
- And I get a series of errors:
Program.cs(6,9): error CS0246: The type or namespace name 'Random' could not be found (are you missing a using directive or an assembly reference?) [/home/treehouse/workspace/workspace.csproj]
Program.cs(6,32): error CS0246: The type or namespace name 'Random' could not be found (are you missing a using directive or an assembly reference?) [/home/treehouse/workspace/workspace.csproj]
Program.cs(10,32): error CS0103: The name 'Math' does not exist in the current context [/home/treehouse/workspace/workspace.csproj]
Program.cs(12,9): error CS0103: The name 'Console' does not exist in the current context [/home/treehouse/workspace/workspace.csproj]
- With the
using System;
directive gone, and without theSystem
namespace in front of each type name, C# can no longer find those types. - A couple of the errors even give you a hint: "are you missing a using directive?"
- So let me undo my changes to add the
using System;
directive back in... - And I'll save and re-run the program.
- Everything's back to working!
Now let me try a using
directive with the unit test.
- This namespace is really long, so I'm just going to copy it from the main code:
Microsoft.VisualStudio.TestTools.UnitTesting
- Then I'll go to the top of the file...
- And type
using
, a space, paste the namespace, and end the statement with a semicolon. - Then I'll go through the rest of the file, and each place the namespace occurs, I can delete it along with the dot that follows it.
- Let me save my changes...
- And now I can run the test:
dotnet test
- I was able to remove that ugly repeated namespace from the code, and everything still works!
using Microsoft.VisualStudio.TestTools.UnitTesting;
[TestClass]
public class UnitTestEquality
{
[TestMethod]
public void TestEquality()
{
Assert.AreEqual(2, 2);
Assert.AreEqual(5, 5);
}
}
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up