Junior — Middle
Mida tähendab ad-hoc-polümorfism programmeerimises?
sobes.tech AI
Vastus AI-lt
Ad-hoc polymorphism is the ability of a function or operator to work with different data types, but with a different implementation for each type. In C#, this is achieved through method or operator overloading. For example, a method with the same name but different parameters implements ad-hoc polymorphism.
Example:
void Print(int number) {
Console.WriteLine("Integer: " + number);
}
void Print(string text) {
Console.WriteLine("String: " + text);
}
// Call
Print(5); // Calls the first method
Print("hello"); // Calls the second method
Thus, the same function adapts to different data types with different implementations.