不能理解带有可选参数的方法的输出

时间:2022-10-28 10:51:48

Today I was writing a small program to understand the fundamentals of Optional Parameters of C#.

今天我编写了一个小程序来理解c#可选参数的基本原理。

Following is the program:

下面是程序:

abstract class AbstractClass
{
    internal abstract void Test();
}

sealed class DerivedClass : AbstractClass
{        
    internal override void Test()
    {
        Console.WriteLine("In override DerivedClass.Test() method");
    }

    internal void Test(int a = 1)
    {
        Console.WriteLine("In DerivedClass.Test(int a=1) method " + a);
    }

    internal void Test(int b, int a = 1)
    {
        Console.WriteLine("In DerivedClass.Test(int b, int a=1) method " + string.Format("{0} {1}", b, a));
    }
}

This is how I called Test() method:

这就是我所谓的Test()方法:

   DerivedClass d = new DerivedClass();       
   d.Test();
   d.Test(6);
   d.Test(b:7);

Output :

输出:

In DerivedClass.Test(int a=1) method 1

在DerivedClass。测试1 = 1(int)方法

In DerivedClass.Test(int a=1) method 6

在DerivedClass。6测试= 1(int)方法

In DerivedClass.Test(int b, int a=1) method 7 1

在DerivedClass。测试方法(int b, int a=1

Regarding d.Test();: Here my understanding is, it will treat Test() as method with an optional parameter, and will invoke Test(int a = 1) with this output:

在这里,我的理解是,它将Test()作为具有可选参数的方法,并将调用Test(int a = 1),输出如下:

In DerivedClass.Test(int a=1) method 1

在DerivedClass。测试1 = 1(int)方法

But this is what confuses me when executing d.Test(6);: Why this method call isn't giving output as:

但是,这就是在执行d.Test(6)时让我感到困惑的地方:为什么这个方法调用没有输出为:

In DerivedClass.Test(int b, int a=1) method 6 1

在DerivedClass。测试方法(int b, int a=1

As per my understanding "6" is the mandatory parameter and it should invoke

根据我的理解,“6”是强制参数,应该调用它

internal void Test(int b, int a = 1)

Kindly explain what is wrong with my understanding.

请解释一下我的理解有什么问题。

Also how to call an overriden method?

如何调用overriden方法?

internal override void Test()

2 个解决方案

#1


6  

The rule matching for internal void Test(int a = 1) matches your code d.Test(6); best: it matches on the number of arguments, the types. That makes the method the best match.

内部void测试的规则匹配(int a = 1)匹配您的代码d.Test(6);最佳:它匹配参数的数量和类型。这使方法成为最佳匹配。

When calling d.Test(b:7);, you force it to run as the last method, since you are matching on the name of the parameter. That makes the last method the best matching.

当调用d.Test(b:7)时,您强制它作为最后一个方法运行,因为您正在匹配参数的名称。这使得最后的方法是最好的匹配。

The first (d.Test();) doesn't match on the method you expected (void Test()), since 'own' methods are preferred over derived methods. Try to remove the base class or use the new operator on the method and you will see.

第一个(d.Test();)与您期望的方法(void Test())不匹配,因为“自己”方法比派生方法更受欢迎。尝试删除基类或使用方法上的新操作符,您将看到。

#2


0  

The rules around optional parameters can be a little confusing, but a simple rule of thumb to remember is that it'll always use a method with less parameters in preference to one with more.

关于可选参数的规则可能有点混乱,但要记住的一个简单的经验规则是,它总是使用参数较少的方法,而不是参数较多的方法。

So for

因此,对于

d.Test(6);

the method with just one parameter:

只有一个参数的方法:

internal void Test(int a = 1)

will be used, even though it's an optional parameter.

将被使用,即使它是一个可选参数。

#1


6  

The rule matching for internal void Test(int a = 1) matches your code d.Test(6); best: it matches on the number of arguments, the types. That makes the method the best match.

内部void测试的规则匹配(int a = 1)匹配您的代码d.Test(6);最佳:它匹配参数的数量和类型。这使方法成为最佳匹配。

When calling d.Test(b:7);, you force it to run as the last method, since you are matching on the name of the parameter. That makes the last method the best matching.

当调用d.Test(b:7)时,您强制它作为最后一个方法运行,因为您正在匹配参数的名称。这使得最后的方法是最好的匹配。

The first (d.Test();) doesn't match on the method you expected (void Test()), since 'own' methods are preferred over derived methods. Try to remove the base class or use the new operator on the method and you will see.

第一个(d.Test();)与您期望的方法(void Test())不匹配,因为“自己”方法比派生方法更受欢迎。尝试删除基类或使用方法上的新操作符,您将看到。

#2


0  

The rules around optional parameters can be a little confusing, but a simple rule of thumb to remember is that it'll always use a method with less parameters in preference to one with more.

关于可选参数的规则可能有点混乱,但要记住的一个简单的经验规则是,它总是使用参数较少的方法,而不是参数较多的方法。

So for

因此,对于

d.Test(6);

the method with just one parameter:

只有一个参数的方法:

internal void Test(int a = 1)

will be used, even though it's an optional parameter.

将被使用,即使它是一个可选参数。