

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Overloaded method and ambiguity in C#
With method overloading, you can have multiple definitions for the same function name in the same scope. The definition of the function must differ from each other by the types and/or the number of arguments in the argument list.
Let us see an example. In this the call would go to the method with a single parameter −
Example
using System; class Student { static void DisplayMarks(int marks1 = 90) { Console.WriteLine("Method with one parameter!"); } static void DisplayMarks(int marks1, int marks2 = 95) { Console.WriteLine("Method with two parameters!"); } static void Main() { DisplayMarks(97); } }
Now let us see what creates an ambiguous call. Here the confusion is that the second method would need two arguments as defaults, whereas the first methid needs one argument to be defaulted. This creates ambiguity.
Example
using System; class Student { static void DisplayMarks(int marks1 = 90, int marks2 = 80) { Console.WriteLine("Method with two parameters!"); } static void DisplayMarks(int marks1, int marks2 = 80, marks3 = 98) { Console.WriteLine("Method with three parameters!"); } static void Main() { DisplayMarks(80); } }
- Related Questions & Answers
- Method Overloading and Ambiguity in Varargs in Java
- Pass long parameter to an overloaded method in Java
- What are overloaded indexers in C#?
- Can main() be overloaded in C++?
- What are the different ways for a method to be overloaded in C#?
- If I change the return type, will the method gets overloaded in java?
- What do you mean by ambiguity in grammar in TOC?
- Operators that cannot be overloaded in C++
- Functions that cannot be overloaded in C++
- Functions that can’t be overloaded in C++
- Hiding of all overloaded methods in base class in C++
- How to implement Python __lt__ __gt__ custom (overloaded) operators?
- Use overloaded methods to print array of different types in Java
- Collections.replaceAll() method and List.replaceAll() method in Java
- Difference between Method Overriding and Method Hiding in C#
Advertisements