using and namespace
- Game Development course
- Beginner
- Lesson 26/83
- 4 min
Learn the using directive and the namespace concept.
The namespace Concept
In C#, namespaces (namespace) are used to group related classes into a logical collection and prevent naming conflicts. In large projects or libraries, two classes might share the same name. Using namespaces, we place classes under their own dedicated packages to distinguish them.
namespace OyunSistemi
{
public class Dusman { }
}All core game classes provided by Unity reside inside the UnityEngine namespace.
The using Directive
To use classes from a namespace without typing their full paths every time, the using directive is added at the very top of the file. For example, when you write using UnityEngine;, you can type GameObject instead of UnityEngine.GameObject. For standard C# libraries, using System; is included.
using System;
using UnityEngine;
public class Karakter : MonoBehaviour
{
}Missing using directives cause compiler errors because the compiler cannot locate the referenced classes.
Practice Include namespace with using
At the top of the file, write the using directive to include the System namespace. Inside the Main method, print "using System basarili!" using Console.WriteLine.
Starter code
// System ad alanini dahil et
public class Program
{
public static void Main()
{
// Console.WriteLine ile mesaj yazdir
}
}Sample solution
using System;
public class Program
{
public static void Main()
{
Console.WriteLine("using System basarili!");
}
}Expected output
using System basarili!
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 What is the primary purpose of using a namespace in C#?
- To logically group classes and avoid name collisions Correct answer
- To speed up lighting calculations in the scene
- To allow code to run directly without compilation
- To automatically set the game's screen resolution
Explanation: Namespaces categorize classes and prevent identically named classes across different libraries from colliding.
2 Importing Unity namespace
using ____;
using UnityEngine;
Explanation: The UnityEngine namespace is imported to use the core classes of the Unity engine and the MonoBehaviour structure.
3 using and namespace matching
| Term | Meaning |
|---|---|
using |
Adds the specified namespace to the file |
namespace |
Namespace block that groups classes |
UnityEngine |
Namespace with Unity engine classes |
System |
Namespace with standard C# libraries |