简体   繁体   English

方法包含默认参数值

[英]Method contains default parameter value

I want to define a function in c# with 2 parameters and they have their default values , like 我想用2个参数在c#中定义一个函数,它们具有其默认值,例如

    public T AccessEntity(string Id = null, string File = null)
    {
        return (from e in ServiceContext.CreateQuery<T>(TableName)
                where e.RowKey == Id || e.File == File
                select e).FirstOrDefault();
    }

Now with this function user can search their record by file or id but if user try to search the record by file then how we can map the first argument to second formal parameter without passing any dummy value as a first argument. 现在,使用此功能,用户可以按文件或id搜索记录,但是如果用户尝试按文件搜索记录,那么我们如何将第一个参数映射到第二个形式参数而不传递任何虚拟值作为第一个参数。

I do not want this : invokingobj.AccessEntity(null, "name of file"); 我不想要这样: invokingobj.AccessEntity(null, "name of file");

Is this possible ? 这可能吗 ?

You can use named arguments : 您可以使用命名参数

invokingobj.AccessEntity(File: "name of file")

Note that you should rename your parameter to file though, to comply with normal .NET naming conventions ("do use camel casing in parameter names"). 请注意,您应该将参数重命名为file ,以符合常规的.NET命名约定 (“在参数名称中使用驼峰式大小写”)。

As an aside, almost everyone - including MSDN - gets confused about the names of the two defaults here. 顺便说一句,几乎所有人(包括MSDN)都对这里的两个默认名称感到困惑。 They are optional parameters (it's the parameter which is decorated with the default value) but named arguments (parameters have always had names - it's arguments that are allowed to specify a name in C# 4). 它们是可选参数 (使用默认值修饰的参数),但具有命名参数 (参数始终具有名称-允许在C#4中指定名称的参数)。

You can invoke the method with named parameters arguments: 您可以使用命名参数 arguments调用该方法:

invokingobj.AccessEntity(File: "name of file");

More information about the use of named arguments an optional parameters can be found in: 有关使用命名参数和可选参数的更多信息,请参见:

Named and Optional Arguments (C# Programming Guide) 命名和可选参数(C#编程指南)

I do not want this : invokingobj.AccessEntity(null, "name of file"); 我不想要这样: invokingobj.AccessEntity(null, "name of file");

You don't have to - you can do : 您不必-您可以:

invokingobj.AccessEntity(File:"name of file");

You can define which parameter to set when you call a function by using the named parameter feature 您可以使用命名参数功能定义调用函数时要设置的参数

使用命名参数

invokingObj.AccessEntity(file: "name of file");

Yes, try this: 是的,请尝试以下操作:

 invokingobj.AccessEntity(File: "name of file");

This language feature is called "named parameters" and is an extension of the well-known "optional parameters". 此语言功能称为“命名参数”,是众所周知的“可选参数”的扩展。

One option is to create several different methods. 一种选择是创建几种不同的方法。 (You could have different overloads if the parameters had a different type, but since they're both string you can't.) (如果参数具有不同的类型,则可能会有不同的重载,但是由于它们都是string ,所以不能。)

public T AccessEntityById(string Id)
{
    return AccessEntity(Id, null);
}

public T AccessEntityByFile(string file)
{
    return AccessEntity(null, file);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM