gpt4 book ai didi

除了类参数的 null 之外,C# 可选参数?

转载 作者:太空狗 更新时间:2023-10-29 18:03:30 26 4
gpt4 key购买 nike

这个问题的最佳解决方案是什么?我正在尝试创建一个函数,该函数具有多个类类型的可选参数,其中 null 是一个有意义的值,不能用作默认值。如,

public void DoSomething(Class1 optional1, Class2 optional2, Class3 optional3)    {        if (! WasSpecified(optional1)) { optional1 = defaultForOptional1; }        if (! WasSpecified(optional2)) { optional2 = defaultForOptional2; }        if (! WasSpecified(optional3)) { optional3 = defaultForOptional3; }        // ... do the actual work ...    }

I can't use Class1 optional1 = null because null is meaningful. I can't use some placeholder class instance Class1 optional1 = defaultForOptional1 because of the compile-time constant requirement for these optional parameters I've come up with the following options:

  1. Provide overloads with every possible combination, which means 8 overloads for this method.
  2. Include a Boolean parameter for each optional parameter indicating whether or not to use the default, which I clutters up the signature.

Has anyone out there come up with some clever solution for this?

Thanks!

edit: I ended up writing a wrapper class for so I didn't have to keep repeating Boolean HasFoo.

    /// <summary>
/// A wrapper for variables indicating whether or not the variable has
/// been set.
/// </summary>
/// <typeparam name="T"></typeparam>
public struct Setable<T>
{
// According to http://msdn.microsoft.com/en-us/library/aa288208%28v=vs.71%29.aspx,
// "[s]tructs cannot contain explicit parameterless constructors" and "[s]truct
// members are automatically initialized to their default values." That's fine,
// since Boolean defaults to false and usually T will be nullable.

/// <summary>
/// Whether or not the variable was set.
/// </summary>
public Boolean IsSet { get; private set; }

/// <summary>
/// The variable value.
/// </summary>
public T Value { get; private set; }

/// <summary>
/// Converts from Setable to T.
/// </summary>
/// <param name="p_setable"></param>
/// <returns></returns>
public static implicit operator T(Setable<T> p_setable)
{
return p_setable.Value;
}

/// <summary>
/// Converts from T to Setable.
/// </summary>
/// <param name="p_tee"></param>
/// <returns></returns>
public static implicit operator Setable<T>(T p_tee)
{
return new Setable<T>
{
IsSet = true
, Value = p_tee
};
}
}

最佳答案

我至少会考虑为参数创建一个新类型:

public void DoSomething(DoSomethingOptions options)

... DoSomethingOptions 可能看起来像这样:

public class DoSomethingOptions
{
private Class1 class1;
public bool HasClass1 { get; private set; }

public Class1 Class1
{
get { return class1; }
set
{
class1 = value;
HasClass1 = true;
}
}

... for other properties ...
}

然后你可以调用它:

DoSomething(new DoSomethingOptions { Class1 = null, Class2 = new Class2() });

您不会以指数级的重载集结束,您仍然可以合理紧凑地调用它。

这类似于 Process 使用 ProcessStartInfo 的方法。

关于除了类参数的 null 之外,C# 可选参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10727395/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com