Namespaces, using directives, global usings (note)
Organize types with namespaces, import namespaces with using (and aliases), avoid name collisions, and note that global using is newer than C# 6.
Namespaces, using directives, global usings (note) is a free C# Academy lesson on CoddyKit. This is lesson 2 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C# Academy learning path, and your progress syncs across the web and the CoddyKit app. The C# Academy course includes 3 lessons in total.
Namespaces & using: overview
Goal: Organize and import code.
- namespace groups related types
- using imports namespaces
- Aliases resolve name clashes
- global using is newer than C# 6 (info only)
Declare namespace & type
Define a namespace to group types; you can always use the fully qualified name to access a type.
using System;
namespace MyApp.Models
{
public class User
{
public string Name { get; private set; }
public User(string name) { Name = name; }
}
}
public class Program
{
public static void Main(string[] args)
{
// Fully-qualified name without using
MyApp.Models.User u = new MyApp.Models.User("Ada");
Console.WriteLine(u.Name);
}
}
using directive basics
Add a using directive to import a namespace so you can use short type names.
using System;
using MyApp.Models; // import the namespace declared previously
namespace MyApp.Models
{
public class User
{
public string Name { get; private set; }
public User(string name) { Name = name; }
}
}
public class Program
{
public static void Main(string[] args)
{
// Short type name thanks to using
User u = new User("Alan");
Console.WriteLine(u.Name);
}
}
Nested namespaces & FQNs
Namespaces can be nested. Use fully qualified names when you want to be explicit.
using System;
namespace Company
{
namespace Billing
{
public class Invoice { public int Number; }
}
namespace Support
{
public class Ticket { public int Id; }
}
}
public class Program
{
public static void Main(string[] args)
{
Company.Billing.Invoice inv = new Company.Billing.Invoice();
inv.Number = 1001;
Company.Support.Ticket t = new Company.Support.Ticket();
t.Id = 42;
Console.WriteLine("Invoice " + inv.Number + ", Ticket " + t.Id);
}
}
using alias
Use a using alias to disambiguate identical type names or to shorten long qualified names.
using System;
// Two classes with the same short name in different namespaces
namespace LibA { public class Logger { public string Name = "A"; } }
namespace LibB { public class Logger { public string Name = "B"; } }
// Create aliases to disambiguate
using ALogger = LibA.Logger;
using BLogger = LibB.Logger;
public class Program
{
public static void Main(string[] args)
{
ALogger a = new ALogger();
BLogger b = new BLogger();
Console.WriteLine(a.Name + " & " + b.Name); // A & B
}
}
Global using note & tips
Note: global using arrived in newer C# (not in C# 6). In C# 6, place using directives at the top of each file.
- Group related types into clear namespaces.
- Keep names short but meaningful.
- Use aliases to resolve collisions.
using directive meaning
Recap
Recap: Use namespace to organize types and using to import them per file. Resolve collisions with aliases. Global using is a newer feature; in C# 6, keep usings at file top.
Learn C# with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 93
- Lessons
- 346
Frequently Asked Questions
Is the “Namespaces, using directives, global usings (note)” lesson free?
Yes — the full text of “Namespaces, using directives, global usings (note)” is free to read here on the web. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C# Academy course, upgrade to CoddyKit PRO. The C# Academy course includes 3 lessons in total.
What will I learn in “Namespaces, using directives, global usings (note)”?
Organize types with namespaces, import namespaces with using (and aliases), avoid name collisions, and note that global using is newer than C# 6. You practise C# Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start C# Academy?
No prior experience is required. C# Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 2 of 3.
How long does the “Namespaces, using directives, global usings (note)” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this C# Academy lesson?
Yes. Every C# Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- public/internal/protected/private (with note on file)
- Namespaces, using directives, global usings (note)
- Partial types & files