Class with same name as namespace

In the same app still (Ilanos), in my Ilanos.Application proj I have a lot of classes that are supposed to use the Joke class. However, for the sake of organization, I'm separating them like this:
|- Ilanos.Application
|- Mappers
|- Joke
|- Ilanos.Application
|- Mappers
|- Joke
The problem of that is the namespace name. The JokeResponseMapper, for example, would be at the namespace Ilanos.Application.Mappers.Joke while the Joke entity/model class is at Ilanos.Core.Entities. The compiler complains that the Ilanos.Application.Mappers.Joke namespace and the Joke class "have the same name", even if they're different, is there a way to keep the organization and have the named classes as they are?
14 Replies
Jimmacle
Jimmacle4mo ago
you'll have to fully qualify the class name (Ilanos.Core.Entities.Joke) to avoid that conflict, i recommend changing your naming instead
Angius
Angius4mo ago
Do you really have multiple mapper classes for a single entity? Alternatively, you can use a type alias
Sun「無用」
Sun「無用」OP4mo ago
yes, for my create command and response also that happened in my service stuff too too late for name changes, so full path it is, ig
Angius
Angius4mo ago
Or a type alias using JokeModel = Ilanos.Core.Entities.Joke; And use JokeModel
Sun「無用」
Sun「無用」OP4mo ago
why isn't there the difference for the compiler, though? that seems a bit counterintuitive
Jimmacle
Jimmacle4mo ago
what do you mean
Sun「無用」
Sun「無用」OP4mo ago
I mean why does the compiler doesn't differenciate namespaces and classes? They're different things
Jimmacle
Jimmacle4mo ago
because it becomes ambiguous what you're refering to
Sun「無用」
Sun「無用」OP4mo ago
yes, but why is it ambiguous, doesn't the compiler have different "memories" for those? that's how I thought it was implemented at least
Jimmacle
Jimmacle4mo ago
it depends exactly on what the code is that's generating the error
Sun「無用」
Sun「無用」OP4mo ago
I was just using the Joke class while a namespace ending in Joke was in scop
namespace Ilanos.Application.Mappers.Joke;

// Ohno, cannot access Joke class
using Ilanos.Domain.Core.Joke;

Joke jk = new();
namespace Ilanos.Application.Mappers.Joke;

// Ohno, cannot access Joke class
using Ilanos.Domain.Core.Joke;

Joke jk = new();
Omnissiah
Omnissiah4mo ago
hey, you can also write global::Namespaces.Joke, imagine that
Sun「無用」
Sun「無用」OP4mo ago
wdym?
Omnissiah
Omnissiah4mo ago
when you have name clashing you can add global:: to identify stuff and global::Joke is funny
Want results from more Discord servers?
Add your server