功能的选择性参数 [英] Selective parameters for functions

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

问题描述

大家好,

我试图了解可选参数和命名参数是如何工作的,到目前为止,我已经设法通过函数实现了这些选项,但是现在我意识到这不是我想要的.

基本上,我想要一个提供选择性值的函数,例如,假设我有一个具有可选值的函数:

Hello everyone,

I am trying to understand how optional and named parameters are work, so far I have managed to implement these options with my function, but now i have realised that this is not what I want.

Basically I want a function that provides selective values, for instance let say I have function with to optional values:

func(string DatabaseName1="db1",string  DatabaseName2="db2")



因此,现在在我的代码中,我在调用方法时可以选择以下选项之一(我不想覆盖或更改其值,该值保持原样(db1,db2)我想选择在这两个之间选择一个).

如果有人对此提供帮助,我将不胜感激.

谢谢,



So now in my code I can choose one of these option when I am calling the method (I don’t want to override or change their value, the value stay as it is (db1,db2)I would like to choose between these two and select one).

I would really appreciate if someone helps me on this.

Thanks,

推荐答案

您可能可以实现一些类似的方法;

You could probably implement something along the lines of this;

private void doSomething(String db1, String db2)
{
    if (!(String.IsNullOrEmpty(db1)))
    {
        //Do this
    }
    else
    {
        if (!(String.IsNullOrEmpty(db2)))
        {
            //Do this
        }
        else
        {
            throw new InvalidOperationException();
        }
    }
}


如果这些值是固定的,并且您不想将其他值作为参数传递,为什么还要使用它们呢?为什么不装一个布尔值?
这不是用于可选参数和命名参数的原因.
首先,请阅读以下内容: http://msdn.microsoft.com/en-us/library/dd264739. aspx [ ^ ]

理论:
如果创建例程(函数,过程,方法等),则在例程头中定义一个正式的参数列表.这些是您在常规正文中使用的名称.在强类型语言中,您还可以定义其所需的类型.调用例程时,将传递实际参数.编译器/解释器必须匹配/绑定它们.有以下几种绑定类型:
-顺序绑定:这很常见,第一个实际参数绑定到第一个形式参数,依此类推.如果语言支持可选参数,则可以跳过它们,并使用默认值.在某些语言中,如果您跳过一种语言,则也必须跳过所有语言.
-名称绑定:系统尝试根据其名称将实际参数绑定到形式参数.因此,在实际参数列表中,必须使用实际参数指定要输入的参数的名称
-类型绑定:系统尝试匹配和/或设置参数的实际类型.自动类型转换可能会发生.

在c#4.0中,您拥有全部.
If these values are fixed, and you don''t want to pass other value as argument, why even use them? Why not a single bool?
This is not what optional and named parameters are made for.

First of all read this: http://msdn.microsoft.com/en-us/library/dd264739.aspx[^]

The theory:
If you create a routine (function, procedure, method or that like), you define a formal parameter list in the routine header. These are the names you use in the routine body. In strongly typed languages you also define their desired type. When you call the routine, you pass the actual parameters. The compiler/interpreter has to match/bind them. There are following types of binding:
- order binding: this is common, the first actual parameter is bound to the first formal parameter and so on. If the language supports optional parameters, you can skip them, and the default value will be used. In some languages if you skip one, you have to skip all to the right of it too.
- name binding: the system tries to bind the actual parameter to the formal parameter based on it''s name. Thus, in the actual parameter list, you have to specify the name of the parameter you want to feed with the actual parameter
- type binding: the system tries to match and/or set the actual type of the parameters. Automatic type cast might occur.

In c# 4.0 you have all.


如何使用null??运算符?例如
How about employing null and the ??-operator? E.g.
// take one or the other argument.
// If a is null, take b; if both are null, take "...".
public void Func(string a = null, string b = null)
{
    string c = a ?? b ?? "...";
    // do something with c...
}



干杯
安迪



Cheers
Andi


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

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