© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•2y ago•
12 replies
dima

Generics question

So guys, I am having a little bit of fun with generics. I need some help though.

Lets say I have a class such as this
public abstract class BaseEntity<T>
{
    public T Id { get; set; }
}
public abstract class BaseEntity<T>
{
    public T Id { get; set; }
}


And I define a Country entity like this
public class Country : BaseEntity<Guid>
{
    // Additional properties and methods specific to Country class
}
public class Country : BaseEntity<Guid>
{
    // Additional properties and methods specific to Country class
}


Country.Id now is Guid.

If I were to implement Currency entity that references Country, I would have to define it like this
public class Currency : BaseEntity<int>
{
    public Guid CountryId { get; set; }
}
public class Currency : BaseEntity<int>
{
    public Guid CountryId { get; set; }
}


Yada-yada-yada, some changes occur. I have X entities that reference Guid CountryId.
Now, I decide its better to make Country have an int Id, so I change it to inherit BaseEntity<int>.
I now also have to replicate that change to X other entities that have CountryId as a reference.

The question: Is there a way to reference a type to achieve something along these lines:
public class Currency : BaseEntity<int>
{
    public typeof(Country.Id) CountryId { get; set; }
}
public class Currency : BaseEntity<int>
{
    public typeof(Country.Id) CountryId { get; set; }
}

This way, when I impact the Country.Id type, it's automatically updated everywhere else 🙂

From what I understand, since all of this is known compile-time, there isn't really a reason why I shouldn't be able to do this, no?
All help appreciated ^_^
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

❔ a question about generics
C#CC# / help
3y ago
Generics, Constraints and a simple question
C#CC# / help
12mo ago
❔ Generics
C#CC# / help
3y ago
❔ Generics
C#CC# / help
3y ago