© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
14 replies
🍦vaniemarshie

Change function behavior depending on class' generic type.

Hello! I was wondering if there was a way to change the behavior of a function in a class with a generic type. I was originally looking into overloading but didn't have any luck. Is what I'm trying to do possible?
public class Lerpable<T>
{
    private readonly T _value1;
    private readonly T _value2;

    public Lerpable<T> (T value1, T value2)
    {
        _value1 = value1
        _value2 = value2
    }

    public T Lerp(float amount)
    {
        // Default behavior, I want to be able to change this depending on what T is.
        return amount > 0.5 ? _value2 : _value1;
    }
}
public class Lerpable<T>
{
    private readonly T _value1;
    private readonly T _value2;

    public Lerpable<T> (T value1, T value2)
    {
        _value1 = value1
        _value2 = value2
    }

    public T Lerp(float amount)
    {
        // Default behavior, I want to be able to change this depending on what T is.
        return amount > 0.5 ? _value2 : _value1;
    }
}
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

❔ Generic class to fire on data change according to data type
C#CC# / help
4y ago
❔ giving current class type to a generic class
C#CC# / help
3y ago
❔ Check What Generic Type a Class Implements
C#CC# / help
4y ago
❔ generic class in generic method
C#CC# / help
3y ago