命名和可选参数 [英] Named and optional arguments

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

问题描述

大家好,



我的目的是对方法进行论证。这个论点将成为一个列表。但是这个参数将被用作可选参数,其默认值应该是一个空列表。我怎么声明呢?



例如

Hello all,

My purpose is to have an argument on a method. This argument is going to be a list. But this argument is going to be used as an optional argument and its default value should be a empty list. How can I declare it?

As an example

public void ExampleMethod(int required, string optionalstr = "default string",
    int optionalint = 10)



使用它


The usage of it

anExample.ExampleMethod(3, optionalint: 4); 



有关详细信息,请参阅 MSDN [ ^ ]

所以我需要类似


For more info you can see it in MSDN[^]
So I need something like

public void ExampleMethod(int required, List<string> optionalstrList = "????",
    int optionalint = 10)

推荐答案

public void Example(List<something> list = null) { /*...*/  }</something>





但我不确定能清楚地了解你的问题。



But I''m not sure to clearly understand what your problem is.


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

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