Skip to content

Covariant returns C# 9.0type safety

Allow a method to return a more specific type than the method it overrides.

Derived class that override methods of the base class are forced to exactly match the method signature of the base class.

In C# 9.0 these rules were relaxed in that a method may return a more specific type than that returned by the base class. This can be useful in fluent builder APIs and visitor patterns.

Code

C#
abstract record CacheOptions
{
    public TimeSpan TimeToLive;
}

record DiskCacheOptions : CacheOptions
{
    public string Path;
}

abstract class Cache
{
    public abstract CacheOptions CreateOptions();
}

class DiskCache : Cache
{
    public override DiskCacheOptions CreateOptions() => new DiskCacheOptions();
}
C#
abstract record CacheOptions
{
    public TimeSpan TimeToLive;
}

record DiskCacheOptions : CacheOptions
{
    public string Path;
}

abstract class Cache<TOptionClass>
{
    public abstract TOptionClass CreateOptions();
}

class DiskCache : Cache<DiskCacheOptions>
{
    public override DiskCacheOptions CreateOptions() => new DiskCacheOptions();
}

// Uses a generic parameter to specify the `CreateOptions` return type
// which works for a builder API + scenarios but fails for things like 
// visitor patterns that have many possible overrides and return types.

Notes

WARNING

This feature only works for base classes - it is not available when implementing an interface.

More information