简体   繁体   English

如果字符串在c#中有空或空格,如何返回false

[英]how to return false if string has null or white space in c#

I cannot return null because my Hlist is a non nullable type. 我不能返回null,因为我的Hlist是一个不可为空的类型。 What else can I return in the place of null? 还有什么我可以在null的地方返回?

HList findHListentry(string letter)
{
    if (string.IsNullOrWhiteSpace(letter))
    {
        HList result = listentry.Find(delegate(HList bk)
                       {
                           return bk.letter == letter;
                       });
        return result;
    }
    else
    {
        return ?;
    }
}

Use a Nullable<HList> instead? 使用Nullable<HList>代替?

HList? findHListentry(string letter)
{
    ///
    return null;
}

There are several approaches of dealing with non-nullable value types: 有几种处理非可空值类型的方法:

  • Using Nullable<HList> (the shorthand name for it is HList? ), or 使用Nullable<HList> (它的简写名称是HList? ),或
  • Defining an "empty" HList entry, similar to how Microsoft defined one for Guid 定义一个“空” HList条目,类似于Microsoft为Guid定义一个条目
  • Make your method return bool instead of HList , and return HList through an out parameter, the way it is done in Dictionary.TryGetValue 让你的方法返回bool而不是HList ,并通过out参数返回HList ,就像在Dictionary.TryGetValue完成它一样。

Using a special value: 使用特殊值:

struct HList {
    public static HList Empty;
    ...
}

if (...) {
    return HList.Empty;
}

Returning bool : 回归bool

bool findHListentry(string letter, out HList res) {
    ...
}

If the input to the method truly shouldn't be null or an empty string, you could throw an exception: 如果方法的输入真的不应null或空字符串,则可能抛出异常:

HList findHListentry(string letter)
{
    if (string.IsNullOrWhiteSpace(letter))
        throw new ArgumentNullException("letter");

    HList result = listentry.Find(
        delegate(HList bk)
        {
            return bk.letter == letter;
        }
    );

    return result;
}

(Note that I also reversed the conditional logic, since it sounded like you were looking for the opposite from the title of the question.) (请注意,我也颠倒了条件逻辑,因为它听起来像是在寻找与问题标题相反的东西。)

You can also look into Code Contracts to verify preconditions for methods instead of manually checking the input and throwing an exception. 您还可以查看代码约定以验证方法的前提条件,而不是手动检查输入并抛出异常。

You have a few options: 你有几个选择:

If HList is a struct: 如果HList是结构:

  1. Make it into a class intead. 使它成为一个类intead。 Then you can return null. 然后你可以返回null。
  2. Change the method signature to HList? findHListentry(string letter) 将方法签名更改为HList? findHListentry(string letter) HList? findHListentry(string letter)

你可能想看看N的Nullable

If you didnt want to return a null, you could create a static instance of HList that's used to indicate it's an 'empty' value. 如果您不想返回null,则可以创建一个HList的静态实例,用于表示它是一个“空”值。

Similar to EventArgs.Empty 与EventArgs.Empty类似

public static readonly HList EmptyHList = new Hlist() { /* initialise */ };

One implementation is to provide an Empty instance of the non-nullable type, and return that in place of null. 一种实现是提供非可空类型的Empty实例,并返回该实例以代替null。 Take string as an example...Whilst String is a nullable type in .NET, it contains a built in, readonly field called Empty, so with String you can do this: 以字符串为例......虽然String是.NET中的可空类型,但它包含一个名为Empty的内置只读字段,因此使用String可以执行以下操作:

if(mystring == null)
{
    //My String Is Null
}

or, you could do this 或者,你可以做到这一点

if(mystring == String.Empty)
{
    //My String is Empty
}

Whilst it is probably not the best approach, you could add an empty instance of HList to your class/struct. 虽然它可能不是最好的方法,但您可以在类/结构中添加一个空的HList实例。 eg 例如

HList findHListentry(string letter)
{
    if (string.IsNullOrWhiteSpace(letter))
    {
        HList result = listentry.Find(delegate(HList bk) { return bk.letter == letter; });
        return result;
    }
    else
    {
        return HList.Empty;
    }
}

public struct HList
{
    public const HList Empty;
}

Now you can do this in place of null 现在你可以用null代替它

if(myHList == HList.Empty)
{
    //My HList is Empty
}

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

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