© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•2y ago•
24 replies
Zeke

Help with Generics

Here is my current code:


StatAttackRange/StatAttackDamage inherit from CharacterStat
private StatAttackRange _attackRange;

public void SetCharacterStats(List<CharacterStat> characterStats)
        {
            foreach (CharacterStat characterStat in characterStats)
            {
                if (characterStat.GetType() == _attackRange.GetType())
                {
                    _attackRange = (StatAttackRange)characterStat;
                }else if (characterStat.GetType() == _attackDamage.GetType())
                {
                    _attackDamage = (StatAttackDamage)characterStat;
                }
            }
        }
private StatAttackRange _attackRange;

public void SetCharacterStats(List<CharacterStat> characterStats)
        {
            foreach (CharacterStat characterStat in characterStats)
            {
                if (characterStat.GetType() == _attackRange.GetType())
                {
                    _attackRange = (StatAttackRange)characterStat;
                }else if (characterStat.GetType() == _attackDamage.GetType())
                {
                    _attackDamage = (StatAttackDamage)characterStat;
                }
            }
        }


I feel like it's inelegant, and I believe the solution is to use generics. But when I try to make a generic function (see below) I'm doing something wrong and I'm not sure what.

https://i.imgur.com/tTW44Y9.png
Imgur
Preview image
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

I need help with inheriting generics
C#CC# / help
4y ago
Problem with Generics
C#CC# / help
3y ago
❔ Builder with generics
C#CC# / help
3y ago
Opperators with generics.
C#CC# / help
4y ago