© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
135 replies
joren

❔ Covariance and Contravariance C#

So I stumbled on this topic and I dont seem to grasp it properly, though I saw the following example somewhere:

class Base
{
  void func() { ... }
}
class Derived : Base
{
  void fuc() { ... }
}

Base a = new Base(); // obv fine
Base b = new Derived(); // obv fine
class Base
{
  void func() { ... }
}
class Derived : Base
{
  void fuc() { ... }
}

Base a = new Base(); // obv fine
Base b = new Derived(); // obv fine


Both now can call
func()
func()
, but
b
b
cannot call
fuc()
fuc()
too as its treated as a
Base
Base
(reference) type. Now in C++, you'd just cast
b
b
to
Derived
Derived
and it'd work and be perfectly valid code. The sources I looked at, mentioned that this is why covariance/contravariance exists?
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
Next page

Similar Threads

✅ Covariance & Contravariance
C#CC# / help
3y ago
Help on Covariance and Contravariance in C#
C#CC# / help
2y ago
❔ binding c# and c++
C#CC# / help
3y ago
❔ Linking C# and C++
C#CC# / help
4y ago