Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is Interface Duck Typing?

Tags:

c#

oop

I heard the word Interface Duck Typing, but do not understand at all what is it? So I read a wiki about this and they said:

In computer programming with object-oriented programming languages, duck typing is a style of typing in which an object's methods and properties determine the valid semantics, rather than its inheritance from a particular class or implementation of an explicit interface. The name of the concept refers to the duck test.

But still could not understand what it. So I saw their program but they use dynamic keyword to call quack() & feather() function of all the classes.

I would request you all please explain in easy way what is Interface Duck Typing and how to implement in C# v2.0 because there is no dynamic keyword.

using System;  namespace DuckTyping  {     public class Duck    {     public void Quack()      {       Console.WriteLine("Quaaaaaack!");     }      public void Feathers()      {       Console.WriteLine("The duck has white and gray feathers.");     }   }    public class Person    {     public void Quack()     {       Console.WriteLine("The person imitates a duck.");     }      public void Feathers()      {       Console.WriteLine("The person takes a feather from the ground and shows it.");     }   }    internal class Program    {     private static void InTheForest(dynamic duck)      {       duck.Quack();       duck.Feathers();     }      private static void Game()      {       Duck donald = new Duck();       Person john = new Person();       InTheForest(donald);       InTheForest(john);     }      private static void Main()      {       Game();     }   } } 
like image 752
Thomas Avatar asked Jan 22 '14 08:01

Thomas


People also ask

Why must we use duck typing?

The main reason for using duck typing is to provide support for dynamic typing in Python programming. In Python, we don't need to specify the variable's data type and we can reassign the different data type values to same variable in further code.

What is duck typing in TypeScript?

According to TypeScript, Duck-Typing is a method/rule used to check the type compatibility for more complex variable types. TypeScript uses the duck-typing method to compare one object with other objects by checking that both objects have the same type matching names or not.

What is duck typing java?

Duck typing in Java. In OO programming, duck typing means an object is defined by what it can do, not by what it is. A statement calling a method on an object does not rely on the declared type of an object, only that the object must implement the method called.

What is duck typing meaning?

Duck typing is a concept related to dynamic typing, where the type or the class of an object is less important than the methods it defines. When you use duck typing, you do not check types at all. Instead, you check for the presence of a given method or attribute.


1 Answers

C# has a nominal type system, so the compatibility of types is done based on their names. In your example you have two classes with a Quack method, however there is no way to write a method which can take instances of these two classes and invoke their Quack method.

In C# 2, the solution would be to introduce an interface and have both classes implement it:

public interface IQuack {     void Quack(); }  public class Duck : IQuack { } public class Human : IQuack { } 

now you can create a method which take an IQuack instance and can call Human.Quack and Duck.Quack through it. In C#, methods are resolved 'early' at compile time, so you need to create a named type which supports the operations the method need so the compilation can succeed. Note there is still a runtime element to calling these methods, since the real implementation of IQuack.Quack needs to be resolved at runtime depending on the real type of the argument.

In a duck-typing system, no attempt is made to validate that a method exists before runtime. All that is required is that a given object supports the operation in that it has the right name and takes the required number of parameters (none in this case), hence the 'if it quacks like a duck' expression.

Duck typing in C# 2 can only be done using reflection, in this case you would accept an object argument and look for the required methods yourself:

public static void MakeQuack(object duck) {     MethodInfo quackMethod = duck.GetType().GetMethod("Quack", Type.EmptyTypes, null);     if (quackMethod!=null)     {         quackMethod.Invoke(duck, new object[] { });     }     else     {         throw new ArgumentException("No Quack() method found on target");     } } 

C#4 makes this much simpler with dynamic:

public static void MakeQuack(dynamic duck) {     duck.Quack(); } 
like image 146
Lee Avatar answered Oct 08 '22 19:10

Lee