Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Multiple dispatch
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
==== C# ==== [[C Sharp (programming language)|C#]] introduced support for dynamic multimethods in version 4<ref>{{cite web |url=https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/types/using-type-dynamic |title=Using type dynamic (C# Programming Guide) |access-date=2020-05-14 }}</ref> (April 2010) using the 'dynamic' keyword. The following example demonstrates multimethods. Like many other statically-typed languages, C# also supports static method overloading.<ref>{{cite web |url=https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/basic-concepts#signatures-and-overloading |title=Basic concepts |access-date=2020-05-14 }}</ref> Microsoft expects that developers will choose static typing over dynamic typing in most scenarios.<ref>{{cite web |url=https://docs.microsoft.com/en-us/archive/msdn-magazine/2011/february/msdn-magazine-dynamic-net-understanding-the-dynamic-keyword-in-csharp-4 |title=Dynamic .NET - Understanding the Dynamic Keyword in C# 4 |date=10 August 2015 |access-date=2020-05-14 }}</ref> The 'dynamic' keyword supports interoperability with COM objects and dynamically-typed .NET languages. [[File:Csharp ColliderLibrary.svg]] The example below uses features introduced in C# 9 and C# 10. <syntaxhighlight lang="c#"> using static ColliderLibrary; Console.WriteLine(Collide(new Asteroid(101), new Spaceship(300))); Console.WriteLine(Collide(new Asteroid(10), new Spaceship(10))); Console.WriteLine(Collide(new Spaceship(101), new Spaceship(10))); string Collide(SpaceObject x, SpaceObject y) => x.Size > 100 && y.Size > 100 ? "Big boom!" : CollideWith(x as dynamic, y as dynamic); // Dynamic dispatch to CollideWith method class ColliderLibrary { public static string CollideWith(Asteroid x, Asteroid y) => "a/a"; public static string CollideWith(Asteroid x, Spaceship y) => "a/s"; public static string CollideWith(Spaceship x, Asteroid y) => "s/a"; public static string CollideWith(Spaceship x, Spaceship y) => "s/s"; } abstract record SpaceObject(int Size); record Asteroid(int Size) : SpaceObject(Size); record Spaceship(int Size) : SpaceObject(Size); </syntaxhighlight> Output: <syntaxhighlight lang="output"> Big boom! a/s s/s </syntaxhighlight>
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)