可选参数和InteropServices [英] Optional arguments and InteropServices

查看:90
本文介绍了可选参数和InteropServices的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用第一时间可选参数,但我无法理解这两个方法定义之间的区别:

I'm using for the firs time optional arguments but I cannot understand difference between those two method definitions:

private void method1([Optional, DefaultParameterValue(string.Empty)] string testString)
{
    //do something
}

private void method2(string testString = "")
{
    //do something
}

方法1的定义需要:

using System.Runtime.InteropServices;

Method2定义较小,不需要导入.

Method2 definition is smaller and needs no import.

在使用其中一种方法语法之前,我是否需要考虑一些问题?

Have I to consider something before using one of those method syntax?

推荐答案

从.NET 1.1开始存在方法1.

Method 1 was present since .NET 1.1.

方法2是C#4引入的(直到那时C#才支持可选参数).

Method 2 was introduced with C# 4 (C# did not support optional parameters until then).

这篇关于可选参数和InteropServices的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆