windows C#-命名实参和可选实参(下)

可选自变量

方法、构造函数、索引器或委托的定义可以指定其形参为必需还是可选。 任何调用都必须为所有必需的形参提供实参,但可以为可选的形参省略实参。

每个可选形参都有一个默认值作为其定义的一部分。 如果没有为该形参发送实参,则使用默认值。 默认值必须是以下类型的表达式之一:

  • 常量表达式;
  • new ValType() 形式的表达式,其中 ValType 是值类型,例如 enum 或 struct;
  • default(ValType) 形式的表达式,其中 ValType 是值类型;

可选参数定义于参数列表的末尾和必需参数之后。 如果调用方为一系列可选形参中的任意一个形参提供了实参,则它必须为前面的所有可选形参提供实参。 实参列表中不支持使用逗号分隔的间隔。 例如,在以下代码中,使用一个必选形参和两个可选形参定义实例方法 ExampleMethod。

复制代码
public void ExampleMethod(int required, string optionalstr = "default string",
    int optionalint = 10)

下面对 ExampleMethod 的调用会导致编译器错误,原因是为第三个形参而不是为第二个形参提供了实参。

复制代码
//anExample.ExampleMethod(3, ,4);

但是,如果知道第三个形参的名称,则可以使用命名实参来完成此任务。

复制代码
anExample.ExampleMethod(3, optionalint: 4);

IntelliSense 使用括号表示可选形参,如下图所示:

此外,还可通过使用 .NET OptionalAttribute 类声明可选参数。 OptionalAttribute 形参不需要默认值。

示例

在以下示例中,ExampleClass 的构造函数具有一个可选形参。 实例方法 ExampleMethod 具有一个必选形参(required)和两个可选形参(optionalstr 和 optionalint)。 Main 中的代码演示了可用于调用构造函数和方法的不同方式。

复制代码
namespace OptionalNamespace
{
    class OptionalExample
    {
        static void Main(string[] args)
        {
            // Instance anExample does not send an argument for the constructor's
            // optional parameter.
            ExampleClass anExample = new ExampleClass();
            anExample.ExampleMethod(1, "One", 1);
            anExample.ExampleMethod(2, "Two");
            anExample.ExampleMethod(3);

            // Instance anotherExample sends an argument for the constructor's
            // optional parameter.
            ExampleClass anotherExample = new ExampleClass("Provided name");
            anotherExample.ExampleMethod(1, "One", 1);
            anotherExample.ExampleMethod(2, "Two");
            anotherExample.ExampleMethod(3);

            // The following statements produce compiler errors.

            // An argument must be supplied for the first parameter, and it
            // must be an integer.
            //anExample.ExampleMethod("One", 1);
            //anExample.ExampleMethod();

            // You cannot leave a gap in the provided arguments.
            //anExample.ExampleMethod(3, ,4);
            //anExample.ExampleMethod(3, 4);

            // You can use a named parameter to make the previous
            // statement work.
            anExample.ExampleMethod(3, optionalint: 4);
        }
    }

    class ExampleClass
    {
        private string _name;

        // Because the parameter for the constructor, name, has a default
        // value assigned to it, it is optional.
        public ExampleClass(string name = "Default name")
        {
            _name = name;
        }

        // The first parameter, required, has no default value assigned
        // to it. Therefore, it is not optional. Both optionalstr and
        // optionalint have default values assigned to them. They are optional.
        public void ExampleMethod(int required, string optionalstr = "default string",
            int optionalint = 10)
        {
            Console.WriteLine(
                $"{_name}: {required}, {optionalstr}, and {optionalint}.");
        }
    }

    // The output from this example is the following:
    // Default name: 1, One, and 1.
    // Default name: 2, Two, and 10.
    // Default name: 3, default string, and 10.
    // Provided name: 1, One, and 1.
    // Provided name: 2, Two, and 10.
    // Provided name: 3, default string, and 10.
    // Default name: 3, default string, and 4.
}

前面的代码演示了一些示例,其中不会正确应用可选形参。 第一个示例说明了必须为第一个形参提供实参,这是必需的。

调用方信息属性

调用方信息属性(例如 CallerFilePathAttribute、CallerLineNumberAttribute、CallerMemberNameAttribute 和 CallerArgumentExpressionAttribute)用于获取方法调用方的相关信息。 在调试或需要记录有关方法调用的信息时,这些属性特别有用。

这些属性是具有编译器提供的默认值的可选参数。 调用方不应为这些参数显式提供值。

相关推荐
大飞pkz1 小时前
【设计模式】题目小练2
开发语言·设计模式·c#·题目小练
csdn_aspnet3 小时前
MongoDB C# .NetCore 驱动程序 序列化忽略属性
mongodb·c#·.netcore
浪扼飞舟3 小时前
c#基础二(类和对象,构造器调用顺序、访问级别、重写和多态、抽象类和接口)
java·开发语言·c#
好望角雾眠6 小时前
第四阶段C#通讯开发-1:通讯基础理论,串口,通讯模式,单位转换,代码示例
开发语言·笔记·c#·串口·通讯
薄荷撞~可乐14 小时前
C#Task(Api)应用
开发语言·c#
almighty2719 小时前
C#WPF控制USB摄像头参数:曝光、白平衡等高级设置完全指南
开发语言·c#·wpf·usb相机·参数设置
❀搜不到19 小时前
查询 conda + pip 装的包
windows·conda·pip
后青春期的诗go19 小时前
金蝶云星空插件开发记录(一)
c#·钉钉·金蝶云星空·插件开发
字符搬运工-蓝天20 小时前
Win7环境中离线安装Visual Studio 2017的相关问题
ide·windows·visual studio
倔强的石头10620 小时前
Windows系统下KingbaseES数据库保姆级安装教程(附常见问题解决)
数据库·windows