क्या वास्तव में अजीब बात है कि मूल्य आप इंटरफ़ेस में वैकल्पिक पैरामीटर के लिए डाल वास्तव में एक फर्क नहीं पड़ता है। मुझे लगता है कि आपको सवाल करना है कि क्या मूल्य एक इंटरफ़ेस विवरण या कार्यान्वयन विवरण है। मैंने बाद वाले लोगों को कहा होगा लेकिन चीजें पूर्व की तरह व्यवहार करती हैं। उदाहरण के लिए निम्नलिखित कोड 1 0 2 5 3 7 आउटपुट करता है।
// Optput:
// 2 5
namespace ScrapCSConsole
{
using System;
interface IMyTest
{
void MyTestMethod(int notOptional, int optional = 5);
}
class MyTest : IMyTest
{
public void MyTestMethod(int notOptional, int optional)
{
Console.WriteLine(string.Format("{0} {1}", notOptional, optional));
}
}
class Program
{
static void Main(string[] args)
{
MyTest myTest1 = new MyTest();
// The following line won't compile as it does not pass a required
// parameter.
//myTest1.MyTestMethod(1);
IMyTest myTest2 = myTest1;
myTest2.MyTestMethod(2);
}
}
}
क्या एक गलती हो रहा है लेकिन यह है कि अगर:
// Output:
// 1 0
// 2 5
// 3 7
namespace ScrapCSConsole
{
using System;
interface IMyTest
{
void MyTestMethod(int notOptional, int optional = 5);
}
interface IMyOtherTest
{
void MyTestMethod(int notOptional, int optional = 7);
}
class MyTest : IMyTest, IMyOtherTest
{
public void MyTestMethod(int notOptional, int optional = 0)
{
Console.WriteLine(string.Format("{0} {1}", notOptional, optional));
}
}
class Program
{
static void Main(string[] args)
{
MyTest myTest1 = new MyTest();
myTest1.MyTestMethod(1);
IMyTest myTest2 = myTest1;
myTest2.MyTestMethod(2);
IMyOtherTest myTest3 = myTest1;
myTest3.MyTestMethod(3);
}
}
}
दिलचस्प बात की तरह है कि यह भी ऐसा ही करने की जरूरत नहीं है अगर आपके इंटरफेस को लागू करने के लिए एक पैरामीटर वैकल्पिक वर्ग बनाता है आप इंटरफ़ेस को लागू करते हैं स्पष्ट रूप से वैकल्पिक मूल्य के लिए कक्षा में जो मान आप देते हैं वह व्यर्थ है। निम्नलिखित उदाहरण में आप मूल्य 9 का उपयोग कैसे कर सकते हैं? संकलन करते समय यह चेतावनी भी नहीं देता है।
// Optput:
// 2 5
namespace ScrapCSConsole
{
using System;
interface IMyTest
{
void MyTestMethod(int notOptional, int optional = 5);
}
class MyTest : IMyTest
{
void IMyTest.MyTestMethod(int notOptional, int optional = 9)
{
Console.WriteLine(string.Format("{0} {1}", notOptional, optional));
}
}
class Program
{
static void Main(string[] args)
{
MyTest myTest1 = new MyTest();
// The following line won't compile as MyTest method is not available
// without first casting to IMyTest
//myTest1.MyTestMethod(1);
IMyTest myTest2 = new MyTest();
myTest2.MyTestMethod(2);
}
}
}
स्रोत
2013-12-19 15:53:00
नीचे मार्टिन की टिप्पणियां विभिन्न नुकसान का एक विस्तृत उदाहरण देती हैं, और मेरी इच्छा है कि संकलक गैर मिलान वाले डिफ़ॉल्ट तर्कों को ध्वजांकित करेगा। उस ने कहा, मैं इसे अपने कोड में उपयोग कर रहा हूं क्योंकि यह अन्य डेवलपर्स के लिए इंटरफ़ेस और कार्यान्वयन स्तर दोनों पर डेवलपर के रूप में मेरा इरादा इंगित करता है। –
दिलचस्प संबंधित प्रश्न, "[क्या इंटरफेस में वैकल्पिक पैरामीटर घोषित करने का कोई कारण है?] (Https: // stackoverflow।कॉम/प्रश्न/6752762 /) ", और," [इंटरफ़ेस पर परिभाषित सी # 4 वैकल्पिक पैरामीटर क्यों कक्षा लागू करने पर लागू नहीं हैं?] (https://stackoverflow.com/questions/4922714/) ", दूसरा दिलचस्प है [लिपर्ट से जवाब] (https://stackoverflow.com/a/4923642/1028230) – ruffin