📖 Scenario: You are organizing a small program that prints messages from different parts of a company. Each part has its own namespace to keep things tidy.
🎯 Goal: You will create namespaces and use using directives to access classes inside them easily. Finally, you will print messages from each namespace.
📋 What You'll Learn
Create two namespaces named Company.Sales and Company.Support
Inside each namespace, create a class named Message with a method GetMessage() that returns a string
Create a Program class with a Main method
Use using directives to access the Message classes from both namespaces
Print the messages returned by GetMessage() methods from both namespaces
💡 Why This Matters
🌍 Real World
In real software projects, namespaces keep code organized and prevent conflicts when many developers work together or when using third-party libraries.
💼 Career
Understanding namespaces and using directives is essential for writing clean, maintainable C# code in professional development environments.
Progress0 / 4 steps
1
Create namespaces and classes
Create two namespaces called Company.Sales and Company.Support. Inside each, create a class named Message with a method GetMessage() that returns the string "Sales department says hello!" for Company.Sales.Message and "Support department is here to help!" for Company.Support.Message.
C Sharp (C#)
Hint
Use the namespace keyword to create namespaces. Inside each, define a public class Message with a GetMessage() method that returns the required string.
2
Add using directives
Add using Company.Sales; and using Company.Support; directives at the top of the file to access the Message classes easily.
C Sharp (C#)
Hint
Place the using directives at the very top of the file before any namespace declarations.
3
Create Program class and call methods
Create a Program class with a Main method. Inside Main, create one object of Message from Company.Sales and one from Company.Support. Call their GetMessage() methods and store the results in variables salesMessage and supportMessage.
C Sharp (C#)
Hint
Use the Program class with a Main method. Create objects for both Message classes. Use the full name Company.Support.Message for the support message object to avoid confusion.
4
Print the messages
Add Console.WriteLine statements to print the variables salesMessage and supportMessage inside the Main method.
C Sharp (C#)
Hint
Use Console.WriteLine to print each message on its own line.
Practice
(1/5)
1. What is the main purpose of a namespace in C#?
easy
A. To group related classes and avoid name conflicts
B. To execute code faster
C. To store variables globally
D. To create user interfaces
Solution
Step 1: Understand what a namespace does
A namespace groups related classes, structs, and other types to organize code logically.
Step 2: Identify the main benefit
Namespaces help avoid name conflicts by separating code into distinct areas.
Final Answer:
To group related classes and avoid name conflicts -> Option A
Quick Check:
Namespace purpose = grouping and avoiding conflicts [OK]
Hint: Namespaces group code to prevent name clashes [OK]
Common Mistakes:
Thinking namespaces speed up code execution
Confusing namespaces with global variables
Assuming namespaces create UI elements
2. Which of the following is the correct syntax to use a namespace in C#?
easy
A. namespace System.Text;
B. use System.Text;
C. import System.Text;
D. using System.Text;
Solution
Step 1: Recall the keyword for importing namespaces
In C#, the keyword to include namespaces is using.
Step 2: Check the syntax format
The correct syntax is using NamespaceName;, so using System.Text; is correct.
Final Answer:
using System.Text; -> Option D
Quick Check:
Keyword for namespaces = using [OK]
Hint: Remember: C# uses 'using' to include namespaces [OK]
Common Mistakes:
Using 'use' or 'import' instead of 'using'
Confusing namespace declaration with usage
Adding semicolon after 'namespace' keyword
3. What will be the output of this code?
using System;
namespace DemoApp {
class Program {
static void Main() {
Console.WriteLine("Hello from DemoApp");
}
}
}
medium
A. Hello from DemoApp
B. Error: Missing using directive
C. No output
D. Hello from System
Solution
Step 1: Analyze the code structure
The code uses the System namespace and defines a DemoApp namespace with a Program class and Main method.
Step 2: Understand the output command
The Console.WriteLine prints the string "Hello from DemoApp" to the console.
Final Answer:
Hello from DemoApp -> Option A
Quick Check:
Console.WriteLine prints the given string [OK]
Hint: Console.WriteLine prints exactly the string inside quotes [OK]
Common Mistakes:
Thinking namespace affects output text
Assuming missing using causes error here
Expecting output from namespace name
4. Identify the error in this code snippet:
namespace MyApp {
class Program {
static void Main() {
Console.WriteLine("Welcome");
}
}
}
medium
A. Incorrect namespace syntax
B. Missing class declaration
C. Missing using directive for System namespace
D. Main method must be public
Solution
Step 1: Check usage of Console.WriteLine
The code uses Console.WriteLine which is inside the System namespace.
Step 2: Verify if System namespace is included
There is no using System; directive, so the compiler won't recognize Console.
Final Answer:
Missing using directive for System namespace -> Option C
Quick Check:
Console requires 'using System;' [OK]
Hint: Always add 'using System;' to use Console class [OK]
Common Mistakes:
Assuming Main must be public (it can be private or default)
Thinking namespace syntax is wrong
Missing class declaration (class is present)
5. You have two classes named Logger in different namespaces: Utils.Logging and App.Services. How can you use both classes in the same file without name conflicts?
hard
A. Put both classes in the global namespace
B. Use full names with namespaces or alias one namespace with 'using Alias = ...;'
C. Use only one namespace at a time
D. Rename one class to avoid conflict
Solution
Step 1: Understand the problem of same class names
Two classes named Logger in different namespaces cause conflicts if used without qualification.
Step 2: Use full names or aliasing
You can use full names like Utils.Logging.Logger and App.Services.Logger or create an alias with using LogUtils = Utils.Logging; to differentiate.
Final Answer:
Use full names with namespaces or alias one namespace with 'using Alias = ...;' -> Option B
Quick Check:
Alias or full names avoid class name conflicts [OK]
Hint: Use aliases or full names to avoid class name clashes [OK]
Common Mistakes:
Thinking you must rename classes physically
Assuming you cannot use both namespaces together
Ignoring namespace qualification
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.