C#4.0中的OptionalAttribute和可选参数有什么区别 [英] What is the difference between OptionalAttribute and optional parameters in C# 4.0

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

问题描述

我正在研究别人的代码,有这样一种方法:

I'm researching someone else's code and there is a method like this:

public SomeClass DoSomething(string param1, [Optional, DefaultParameterValue("")] string optional)

为什么有人会使用这些属性代替

Why would someone use these attributes instead of

public SomeClass DoSomething(string param1, string optional = "")

在行为上有什么不同吗?

Is there any difference in the behavior, etc.?

推荐答案

例如,如果他们没有使用C#4,该怎么办?我相信第二个版本将完全编译成第一个版本...

If they weren't using C# 4, for example? I believe the second version will compile into exactly the first version...

(我都编译了它们,并通过ILDASM运行它们-没有显着差异.)

(I've compiled them both and run them through ILDASM - there were no significant differences.)

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

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