Follow us on X (Twitter)  Like us on Facebook  Connect with us on LinkedIn  Subscribe to our YouTube Channel  Subscribe to our WhatsApp Group

What is Duck Typing? You might know about it already!


Duck typing is a style of programming where it allows an object to be passed in to a method that expects a certain type even if it doesn’t inherit from that type. Let's start discussing about this in today's interview question series. - Article authored by Kunal Chowdhury on .

“Duck Typing” is not a common interview question but one of the interviewer in a panel may ask you this just to check your knowledge out of the common questionnaire. You might be already answered this in a different question but stucked here.

 

So, what is this “Duck Typing” and how this benefits a C# developer? Ok, let’s start discussing this in today’s blog post and I assume, you will find it interesting.

 

Duck typing is a style of programming where it allows an object to be passed in to a method that expects a certain type even if it doesn’t inherit from that type. All it has to do is support the methods and properties of the expected type in use by the method. In duck typing, one is concerned with just those aspects of an object that are used, rather than the type of the object itself.

 

If the object does not have the methods that are called then it will signal a run-time error to the user. If the object have the methods, then they are executed no matter the type of the object.

 

 

Let’s look into the following example:

 

Duck Typing

 

“An animal is a duck when it walks like a duck and quack like a duck” – this is very popular phrase to us while defining “Duck Typing”. In the above example, suppose we have a method that takes a duck instance and another method that takes a person instance. Now, if my programming language supports Duck Typing, I can use the first method to pass both the object as long as it supports all the properties and methods defined in the person instance.

 

This is usually done using a programming language that supports dynamic language. Few of them which supports dynamic programming are: C#, Cobra, ColdFusion, JavaScript, Groovy, Perl, PHP, PowerShell, Python, Ruby etc.

 

C# as a Dynamic Programming Language

 

Let’s come to our favourite C# language in .NET, where we will see how to implement the Duck Typing in our application. As you became familiar with the duck typing, so let’s see how we can achieve that. You already know about the “dynamic” keyword, am I right? Yes, that uses the duck typing. Winking smile

 

If you are not aware of the properties and/or methods of an object, you can use the dynamic keyword and call that from your code. If the object has that property/method already defined, it will execute properly but in case it’s not there will result a runtime error to the user.

 

Let us take an example of this code (as stated above):

 

public void DuckTyping()
{
    dynamic obj = new Duck();
    obj.Walk();  // will execute properly as the Duck instance has Walk() method
    obj.Quack(); // will execute as Quack() method is know to Duck instance
 
    obj = new Person();
    obj.Walk();  // will execute properly as the Person instance has Walk() method
    obj.Quack(); // will throw a runtime error as the Quack() is unknown to Person
}

 

 

When you use a “dynamic” keyword, the compiler will not check for the property or method names available in the class, rather it provides permission to the runtime to decide the object type and directly use it there.

 

I hope that this post helped you to understand the term “Duck Typing” and how we can use dynamic keyword in C# to implement it when we don’t know more about the property and method of the class. If you have further queries on this, let’s discuss about it more below.

Have a question? Or, a comment? Let's Discuss it below...

Thank you for visiting our website!

We value your engagement and would love to hear your thoughts. Don't forget to leave a comment below to share your feedback, opinions, or questions.

We believe in fostering an interactive and inclusive community, and your comments play a crucial role in creating that environment.