usingSystem;classExample{publicExample(int a) {Console.WriteLine("Constructor with int"); }publicExample(double a) {Console.WriteLine("Constructor with double"); }}classProgram{staticvoidMain() {Example ex =newExample(5.0f); // Ambiguous overload }}
ในตัวอย่างนี้ constructor ตัวแรก Example(int a) รับพารามิเตอร์ประเภท int และ constructor ตัวที่สอง Example(double a) รับพารามิเตอร์ประเภท double เมื่อเราสร้างอินสแตนซ์ Example โดยส่งค่า 5.0f ซึ่งเป็นค่า float คอมไพเลอร์สามารถแปลงค่า float เป็น int ได้โดย implicit conversion เพื่อใช้คอนสตรัคเตอร์ตัวแรก และ ใน double ก็สามารถทำได้เช่นกัน เนื่องจากทั้งสอง constructor สามารถรับค่าที่ส่งเข้ามาได้ทั้งคู่ คอมไพเลอร์จึงไม่สามารถเลือก constructor ที่เหมาะสมได้ ทำให้เกิดปัญหา Ambiguous Overload ขึ้น
วิธีแก้ปัญหา
//เป็นการบังคับให้ใช้ constructor ที่รับค่าเป็น int
Example ex = new Example((int)5.0f);
//เพิ่ม constructor ที่รับค่าเป็นชนิดข้อมูลที่ต้องการ
public Example(float a)
{
Console.WriteLine("Constructor with float");
}
using System;
class Multiple {
int x, y;
int f, p, s;
public Multiple(int a, int b)
{
x = a;
y = b;
}
public Multiple(int a, int b, int c)
{
f = a;
p = b;
s = c;
}
public void show()
{
Console.WriteLine("Fisrt constructor (int * int): {0} ", (x * y));
}
public void show1()
{
Console.WriteLine("Second constructor (int * int * int): {0}", (f * p * s));
}
}
class Mul {
static void Main()
{
Multiple g = new Multiple(10, 20);
g.show();
Multiple q = new Multiple(10, 20, 30);
q.show1();
}
}
Fisrt constructor (int * int): 200
Second constructor (int * int * int): 6000
using System;
class ADD {
int x, y;
double f;
string s;
public ADD(int a, double b)
{
x = a;
f = b;
}
public ADD(int a, string b)
{
y = a;
s = b;
}
public void show()
{
Console.WriteLine("First constructor (int + float): {0} ", (x + f));
}
public void show1()
{
Console.WriteLine("Second constructor (int + string): {0}", (s + y));
}
}
class A {
static void Main()
{
ADD g = new ADD(20, 10.5);
g.show();
ADD q = new ADD(10, "The value of y is ");
q.show1();
}
}
First constructor (int + float): 30.5
Second constructor (int + string): The value of y is 10
จากตัวอย่าง คลาสนี้มีชื่อว่า ADD ใน constructor ตัวแรกมีพารามิเตอร์เป็น int และ double ส่วน constructor ตัวที่สอง มีพารามิเตอร์เป็น int และ string ซึ่งจะเห็นได้ว่าจำนวนของพารามิเตอร์เท่ากัน แต่ชนิดของพารามิเตอร์ต่างกัน
3.By changing the Order of the parameters
using System;
class student {
public string name;
public double height;
public double weight;
public student(string n, double h, double w)
{
name = n;
height = h;
weight = w;
}
public student(double h, double w, string n)
{
height = h;
weight = w;
name = n;
}
public void show()
{
Console.WriteLine("Name : " + name);
Console.WriteLine("Height : " + height + " cm.");
Console.WriteLine("Weight : " + weight + " kg.");
}
}
class std {
static void Main()
{
student s1 = new student("Natcha", 164.5 , 75.6);
student s2 = new student(183.3, 69.2, "Thanakorn");
Console.WriteLine("First Constructor: ");
s1.show();
Console.WriteLine();
Console.WriteLine("Second Constructor: ");
s2.show();
}
}
First Constructor:
Name : Natcha
Height : 164.5 cm.
Weight : 75.6 kg.
Second Constructor:
Name : Thanakorn
Height : 183.3 cm.
Weight : 69.2 kg.
using System;
class Employee
{
public string Name;
public int Id;
public int Age;
public string Position;
public Employee()
{
Name = "Unknown";
Id = 0;
Age = 0;
Position = "Not Assigned";
}
public Employee(string name, int id)
{
Name = name;
Id = id;
Age = 0;
Position = "Not Assigned";
}
public Employee(string name, int id, int age, string position)
{
Name = name;
Id = id;
Age = age;
Position = position;
}
}
class emp
{
static void Main()
{
Employee emp1 = new Employee();
Employee emp2 = new Employee("somjai", 101);
Employee emp3 = new Employee("somdej", 102, 30, "Manager");
Console.WriteLine($"{emp1.Name}, {emp1.Id}, {emp1.Age}, {emp1.Position}");
Console.WriteLine($"{emp2.Name}, {emp2.Id}, {emp2.Age}, {emp2.Position}");
Console.WriteLine($"{emp3.Name}, {emp3.Id}, {emp3.Age}, {emp3.Position}");
}
}