繁体   English   中英

C#4.0 - 如何处理可选的字符串参数

[英]C# 4.0 - How to Handle Optional String Parameters

此代码无效:

private void Foo(string optionalString = string.Empty)
{
   // do foo.
}

但是这段代码是:

private void Foo(string optionalString = "")
{
   // do foo.
}

为什么? 因为string.Empty是只读字段,而不是常量,并且可选参数的缺省值必须是编译时常量。

那么,关于我的问题......(好吧,关心)

这就是我必须做的事情:

private const string emptyString = "";

private void Foo(string optionalString = emptyString)
{
   // do foo.
   if (!string.IsNullOrEmpty(optionalString))
      // etc
}

你们如何处理可选的字符串参数?

为什么它们不能使String.Empty成为编译时常量?

嗯...字符串optionalParm =“”又出了什么问题? 为什么那么糟糕? 在这种情况下,你真的认为你需要一个空字符串的符号常量吗? 那怎么样?

const int Zero = 0;

void SomeMethod(int optional = Zero) { }

这对你来说真的很傻吗?

如果您不喜欢“”值,则可以使用默认值(字符串)。
我玩它并且允许。

private static void foo(string param = default(string)) {
    if (!string.IsNullOrEmpty(param)) // or param != default(string)
        Console.WriteLine(param);
}

处理它们的最佳方法是:

private void Foo(string optionalString = "")
{
   // do foo.
}

所以你不能使用String.Empty。 每个人都认识到“”,但如果我发现optionalString = nullString我不知道该怎么想。 如果没有别的,请将事物命名为emptyString - 它不是空的!

代码分析警告1026表示不使用可选参数。 使用重载方法是更好的样式,如下所示:

private void Foo()
{
   Foo(string.Empty);
}
private void Foo(string optionalString)
{
   // do foo.
   if (!string.IsNullOrEmpty(optionalString))
      // etc
}

我正在回答这个问题。

Why can they not make String.Empty a compile-time constant?

这是通过mscorlib.dll中String.cs的Reflector的反汇编代码

public static readonly Empty;
static String()
{
    Empty = "";
    WhitespaceChars = new char[] { 
        '\t', '\n', '\v', '\f', '\r', ' ', '\x0085', '\x00a0', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 
        ' ', ' ', ' ', ' ', '​', '\u2028', '\u2029', ' ', ''
     };
}

所以在windows平台上,string.Empty就是“”。 但是你知道吗,Martian在他们的操作系统中对Empty和WhitespaceChars有不同的定义。

如果你愿意玩lost并将null,“”和空白字符视为相同,那么你可以默认为null 当用户名和密码是可选字段时,由于可能与数据库建立可信连接,这变得非常方便。 您可以更改此逻辑以将字符串重置为null ,从而修改断言和if 重要的是拥有一致的惯例。

private void RunSql(string serverName, string databaseName, string userName = null, string password = null)
{
    userName = Strip(userName);
    password = Strip(password);

    // The `MsTest` assert - works in both `Debug` and `Release` modes.
    Assert.AreEqual<bool>(
        userName == String.Empty,
        password == String.Empty,
        "User name and password should be either both empty or both non-empty!");
   Assert.IsFalse(String.IsNullOrWhiteSpace(serverName));
   Assert.IsFalse(String.IsNullOrWhiteSpace(databaseName));

   var cmdBuilder = new StringBuilder();
   cmdBuilder.AppendFormat("sqlcmd -E -S {0} -d {1} ", serverName, databaseName);
   if (userName.Length > 0)
   {
       cmdBuilder.AppendFormat("-U {0} -P {1} ", userName, password);
   }

   // Complete the command string.
   // Run the executable.
}

// Cannot think of a good name. Emptify? MakeNullIfEmpty?
private string Strip(string source)
{
    if (String.IsNullOrWhiteSpace(source))
    {
        return String.Empty;
    }

    return source;
}

暂无
暂无

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

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