C# using
Keep namespace Pack with public class Box, a public int field size, and public Box(int n) that sets size = n;. Near the top of the file, after using System;, add using Pack;. In Pine, keep Main and build one box with the short name Box b = new Box(4);, then write b.size. Keep everything in the same teaching file so dotnet run stays simple. Run with dotnet run. The screen should show one line: 4. The using directive is what lets Pine drop the Pack. prefix. Keep the starting public class named Pine.
A directive that shortens the type name
On the namespaces page you wrote Pack.Box every time. That still works. This page adds using Pack; so the compiler treats bare Box as the class inside Pack. You still keep the namespace around the type. You only change how Main spells the name.
Open the Pine file. The starting class name stays Pine. This page does not rename the starter. Delete any Pack.Box b = new Pack.Box(5); leftover if that long-name demo is still all you have in Main. You may keep the Pack / Box type itself. Delete any Lid leftover. Delete any ReadLine line. The file you keep has no wait for typing.
At the top, keep using System;. On the next line write using Pack;. Then keep namespace Pack { public class Box { public int size; public Box(int n) { size = n; } } }. Outside that namespace, keep public class Pine. Inside Main, write the short form Box b = new Box(4); and Console.WriteLine(b.size);. Save before you run.
Do not put using Pack; inside Main. Directives sit at the top of the file with using System;. Do not nest Pine inside namespace Pack on this page.
using System;
using Pack;
namespace Pack
{
public class Box
{
public int size;
public Box(int n)
{
size = n;
}
}
}
public class Pine
{
public static void Main()
{
Box b = new Box(4);
Console.WriteLine(b.size);
}
}
Pine calls Box after using Pack
Pine keeps public static void Main(). Because using Pack; is present, Box means the class in that namespace. The constructor stores 4. The write shows that field. You are not required to write Pack.Box on this page. The long form still compiles if you use it, but the lesson goal is the short name after the directive.
using System;
using Pack;
namespace Pack
{
public class Box
{
public int size;
public Box(int n)
{
size = n;
}
}
}
public class Pine
{
public static void Main()
{
Box b = new Box(4);
Console.WriteLine(b.size);
}
}
Read the kept file from the top. using System; prepares console names. using Pack; brings the Pack namespace into short-name reach. namespace Pack still wraps public class Box. After the namespace closes, public class Pine is the starter. public static void Main() is where the runtime starts. The expression new Box(4) builds the same kind of object you built before as new Pack.Box(5) on the previous page. Then Main writes b.size and the screen shows 4.
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 Box b = new Box(4); plus the write. The screen shows 4. If the screen shows 5, a leftover long-name demo with argument 5 may still be running. If the build cannot find Box, using Pack; may be missing — put it back under using System;.
If you write only using System; and then Box b = new Box(4); without using Pack;, the build refuses unless you go back to Pack.Box. This page wants both the directive and the short name.
Compared with Pack.Box and with forgetting the directive
On the namespaces page, every construction used Pack.Box. That long form remains legal. This page’s checkpoint is using Pack; plus bare Box. A leftover Pack.Box b = new Pack.Box(5); without practicing the short name skips the new habit. For this page keep the directive and write Box b = new Box(4);.
using System; is not the same as using Pack;. You already needed System for Console. The new line is specifically for your Pack group. Leaving using Pack; out while deleting the Pack. prefix is the common mistake on this page.
Two short Box names in one Main
You can build two boxes with the short name after the same directive. Write Box a = new Box(1); and Box b = new Box(6);, then write both a.size and b.size. You do not need a second using Pack;. One directive covers the file.
If you still prefer Pack.Box for one of the lines, that mixes styles. For the teaching check, use the short name on both constructions so the directive is doing real work.
What this leaves for list
This page shortens a namespace-qualified type with using Pack;. A later page — list — starts the collections lane with List for a growable sequence of values. Stay with using here. Do not invent list demos on this page unless you already know them; the lesson goal is the directive and the short Box name.
Do not put Main inside Box. Do not leave a Lid demo in Main. If the build cannot find Box, check using Pack; at the top and namespace Pack around the class. If the screen shows 0, the constructor may be missing the store — keep public Box(int n) { size = n; } before the write.
You can keep namespace Pack { public class Box { public int size; public Box(int n) { size = n; } } }, write using Pack; under using System;, and from Pine.Main run Box b = new Box(4);, then write b.size. Run with dotnet run. The screen shows 4. The directive made the short name work. Think of using Pack; as a note that says “when I write Box, I mean the one on the Pack shelf.” Pine stays the door you open with dotnet run. The next page is list, where you store several values in a growable collection instead of one box field.
Keep using Pack; at the top. Keep the short Box name in Main on this page. Keep leftover long-only Pack.Box demos out of the checkpoint so you actually practice the directive. Those habits are the gate before you move on.
Mistakes that hide the using line
Writing using Pack; inside Main is not the file-level directive shape this page teaches. Move it next to using System;. Writing new Box(4) while Box still lives only under Pack and the directive is missing makes the build fail — restore using Pack; or temporarily use Pack.Box only while you debug, then return to the short name for the lesson.
Another trap is deleting using System; while adding using Pack;. Console writing still needs System unless you write fully qualified console calls this page does not require. Keep both using lines. If IntelliSense offers a different Box from another namespace, stay with your Pack type for this file.
🧠 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