Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# multiple dispatch options?

I have these classes :

class Asset
{    }

class House:Asset
{    }

consider these outsiders static functions :

static void Foo (Asset a) { }
static void Foo (House h) { }

If i write :

House h = new House (...); 
Foo(h);

it will call Foo(House)(compile time binding)

if i write :

Asset a = new House (...);
Foo(a); 

it will call Foo(Asset) (compile time binding)

goal : access the runtime type method :

I have 2 options :

1) using dynamic like this :

 Asset a = new House (...);
 Foo ((dynamic)a); // NOW it will call  Foo(House)

2) move the functions from static to override using polymorphism mechanism.

question :

is there any other way of doing it ( without moving the functions to polymorphism mechanism || dynamic) ?

like image 834
Royi Namir Avatar asked Apr 26 '12 09:04

Royi Namir


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.


1 Answers

goal : access the runtime type method

That's what the dynamic keyword is there for. It's actually a pretty really clean & fast way to do multiple dispatch.

Your ultimate options for Multiple Dispatch are

  1. dynamic
  2. Double Dispatch virtual methods
  3. Some hashed anonymous function rule collection
  4. if (x is House) ... else if(x is Asset)...
  5. Reflection -- really slow and ugly

question : is there any other way of doing it ( without moving the functions to polymorphism mechanism || dynamic) ?

So Yes, there are ways of doing that take a lot of work on your part when you could just use dynamic which is fast, less error prone, and really clean syntax wise.

like image 185
jbtule Avatar answered Sep 19 '22 09:09

jbtule