C# Namespaces
Write a namespace Pack that holds public class Box with a public int field size and a constructor public Box(int n) that sets size = n;. In Pine, keep Main and build one box with Pack.Box b = new Pack.Box(5);, then write b.size. Keep the namespace and Pine in the same teaching file so dotnet run stays simple. Run with dotnet run. The screen should show one line: 5. The type lives under the name Pack. Reaching it as Pack.Box is what this page teaches. Keep the starting public class named Pine.
A group name around a type
On the access page you hid seal with private and opened Read. Here the new idea is a namespace. A namespace is a group name around one or more types. Inside namespace Pack { ... } you place public class Box. Outside that group, Pine names the type as Pack.Box, not as a bare Box, on this page.
Open the Pine file. The starting class name stays Pine. This page does not rename the starter. Delete any Lid L = new Lid(9); leftover if that access demo is still all you have in Main. Delete any Lid class leftover. Delete any Cup leftover. Delete any ReadLine line. The file you keep has no wait for typing.
Above or beside Pine — still in the same file — write namespace Pack. Inside its braces write public class Box. Inside Box declare public int size; and public Box(int n) { size = n; }. There is no Main inside Pack on this page. Save the file before you run.
Pine stays outside the Pack braces as a sibling in the file. Do not nest Pine inside namespace Pack on this page. The starter class remains the door dotnet run opens.
using System;
namespace Pack
{
public class Box
{
public int size;
public Box(int n)
{
size = n;
}
}
}
Pine builds Pack.Box
Pine keeps public static void Main(). Inside Main, write two lines in order: Pack.Box b = new Pack.Box(5);, then Console.WriteLine(b.size);. The qualified name Pack.Box points at the class inside the namespace. The constructor stores 5. The write shows that field. You are not writing Lid L = new Lid(9); or L.Read() on this page. That was the access lesson.
using System;
namespace Pack
{
public class Box
{
public int size;
public Box(int n)
{
size = n;
}
}
}
public class Pine
{
public static void Main()
{
Pack.Box b = new Pack.Box(5);
Console.WriteLine(b.size);
}
}
Read the kept file from the top. using System; prepares short names for console work. namespace Pack opens the group. Inside it, public class Box holds size and the constructor. After the namespace closes, public class Pine is the starter. public static void Main() is where the runtime starts. The expression new Pack.Box(5) builds a box under that group name and stores 5 in size. Then Main writes b.size and the screen shows 5.
Run it with dotnet run. The program writes one line and stops. It does not wait for you to type. If dotnet run waits, ReadLine is still in the file. Delete that line and keep Pack.Box b = new Pack.Box(5); plus the write. The screen shows 5. If the screen shows 9, a leftover Lid demo may still be running. If the build cannot find Box, you may have written new Box(5) without the Pack. prefix on this page — put Pack.Box back.
If you nest Pine inside namespace Pack, the starter shape this course uses changes and dotnet run may look for a different entry. Keep Pine outside Pack as a sibling. If the braces of namespace Pack never close, the build refuses — match every opening brace.
Compared with Lid access and with a bare Box name
On the access page, Lid sat in the global teaching file without a namespace wrapper. That still works for small demos. This page places Box under Pack so the full name is Pack.Box. A leftover Lid L = new Lid(9); Console.WriteLine(L.Read()); is a different lesson. For this page keep Pack, Box, and Pine.
📊 short name vs Pack.Box
| How you write the type | Meaning on this page |
|---|---|
Box alone in Main |
not the shape this page keeps — prefer Pack.Box |
Pack.Box |
the Box class inside namespace Pack |
Two boxes under the same namespace
You can build two Pack.Box values with different sizes. Write Pack.Box a = new Pack.Box(2); and Pack.Box b = new Pack.Box(7);, then write both a.size and b.size. The namespace name stays the same. Each object still has its own size.
using System;
namespace Pack
{
public class Box
{
public int size;
public Box(int n)
{
size = n;
}
}
}
public class Pine
{
public static void Main()
{
Pack.Box a = new Pack.Box(2);
Pack.Box b = new Pack.Box(7);
Console.WriteLine(a.size);
Console.WriteLine(b.size);
}
}
Run again with dotnet run. The screen should show 2 on one line and 7 on the next. Each constructor stored its own argument. If both lines show 5, you may still be on the one-box demo — switch to the two-box file when you want that check. If the screen shows 9 or another leftover number, an older Lid demo may still be in Main. Keep the two Pack.Box lines and the two writes.
Writing Pack.Box c = a; does not build a third box. Use another new Pack.Box(...) when you want a separate size. For this page, two constructions are enough to see the qualified name twice.
What this leaves for using
This page reaches the type with a fully qualified name: Pack.Box. A later page — using — shows how a using directive can bring a namespace into short-name reach so you can write Box after declaring using Pack;. Stay with the long name here. Do not invent that shortcut on this page unless you already know it; the lesson goal is the namespace wrapper and Pack.Box.
Do not put Main inside Box. Do not leave Lid in the file. If the build cannot find Pack.Box, check that namespace Pack wraps public class Box and that Pine sits outside those braces. If the screen shows 0, the constructor may be missing the store — keep Box(int n) { size = n; } before the write.
You can write namespace Pack { public class Box { public int size; public Box(int n) { size = n; } } }, and from Pine.Main run Pack.Box b = new Pack.Box(5);, then write b.size. Run with dotnet run. The screen shows 5. The namespace grouped the type. The qualified name told Pine which Box you meant. Think of Pack as a labeled shelf and Box as the item on that shelf. Pine stays the door you open with dotnet run. The next page is using, where a directive can shorten Pack.Box to Box after you opt in.
Keep the namespace braces around Box. Keep Pack. in front of the type name in Main on this page. Keep leftover L.Read() demos out of Main so the screen cannot show a lid number by mistake. Those habits are the checkpoint before you move on.
🧠 Test Your Knowledge
Test Your Knowledge
Challenge yourself with this interactive quiz and see how well you understand the topic
📝 Instructions
- Read each question carefully
- Select the best answer for each question
- You can retake the quiz as many times as you want
- Your progress will be shown at the top