C# Language Using Directive Associate an Alias to Resolve Conflicts

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Example

If you are using multiple namespaces that may have same-name classes(such as System.Random and UnityEngine.Random), you can use an alias to specify that Random comes from one or the other without having to use the entire namespace in the call.

For instance:

using UnityEngine;
using System;

Random rnd = new Random();

This will cause the compiler to be unsure which Random to evaluate the new variable as. Instead, you can do:

using UnityEngine;
using System;
using Random = System.Random;

Random rnd = new Random();

This doesn't preclude you from calling the other by it's fully qualified namespace, like this:

using UnityEngine;
using System;
using Random = System.Random;

Random rnd = new Random();
int unityRandom = UnityEngine.Random.Range(0,100);

rnd will be a System.Random variable and unityRandom will be a UnityEngine.Random variable.



Got any C# Language Question?