用更好的方法检查字符串null C#
一个方法接受大约7个字符串参数,需要检查它们是否为null 对于所有这些问题,最基本的解决方法就是简单!IsNullOrWhitespace(param1)和 有更好或更聪明的方法吗?用更好的方法检查字符串null C#,c#,C#,一个方法接受大约7个字符串参数,需要检查它们是否为null 对于所有这些问题,最基本的解决方法就是简单!IsNullOrWhitespace(param1)和 有更好或更聪明的方法吗? 此外,如果有帮助的话,我可以将参数包装在对象中 如果希望返回任何一个不为空的字符串,那么这是一个很好的解决方案: public static bool AreAllStringsEmpty(params string[] array) { return array.All(str => String
此外,如果有帮助的话,我可以将参数包装在对象中 如果希望返回任何一个不为空的字符串,那么这是一个很好的解决方案:
public static bool AreAllStringsEmpty(params string[] array)
{
return array.All(str => String.IsNullOrWhiteSpace(str));
}
public static bool AreAllStringsFull(params string[] array)
{
return array.All(str => !String.IsNullOrWhiteSpace(str));
}
public static bool AreAtleastOneStringFull(params string[] array)
{
return array.Any(str => !String.IsNullOrWhiteSpace(str));
}
然后像这样使用它:
AreAllStringsFull(param1, param2, ...);
CheckAnyNulls(str1, str2, str3, str4, str5, str6, str7);
CheckAllNulls(str1, str2, str3, str4, str5, str6, str7);
int nullStrs = CheckNullCount(str1, str2, str3, str4, str5, str6, str7);
if(List.All(x=>string.IsNullOrWhiteSpace(x)))
{
}
备注:您可以添加扩展方法来简化流程,但它仍然是样板代码。如果您不需要检查空白,只需将其与null进行比较即可:
if (param1 != null)
按照惯例,如果要求输入不为null,则会抛出:
您可以使用反射构建一个扩展方法,一次检查整个对象。但是,由于这会很昂贵,您可以使用库(如FastMember)来最小化后续对象检查的反射成本
using System.Linq;
using FastMember;
namespace Utilities
{
public static class CheckStringProperties
{
public static bool AreAnyStringPropertiesNull<T>(this T model) where T : class
{
var accessor = TypeAccessor.Create(model.GetType());
return AreAnyStringPropertiesNull(model, accessor);
}
public static bool AreAnyStringPropertiesNull<T>(this T model) where T : class
{
var accessor = TypeAccessor.Create(model.GetType());
return AreAllStringPropertiesNull(model, accessor);
}
private static bool AreAnyStringPropertiesNull<T>(T model, TypeAccessor accessor)
{
foreach (var strng in GetStringProperties(accessor))
{
if (string.IsNullOrWhiteSpace(strng))
return true;
}
return false;
}
private static bool AreAllStringPropertiesNull<T>(T model, TypeAccessor accessor)
{
foreach (var strng in GetStringProperties(accessor))
{
if (!string.IsNullOrWhiteSpace(strng))
return true;
}
return false;
}
private static List<Member> GetStringProperties(TypeAccessor accessor)
{
return accessor.GetMembers().Where(x => x.Type == typeof(string)).ToList();
}
}
}
如果任何
为空
对您来说将是一个错误:
bool CheckAnyNull(params string[] strs)
{
foreach (var str in strs)
{
if (strs.IsNullOrWhiteSpace(str))
return true;
}
return false;
}
如果要检查它们是否为全部空
:
bool CheckAllNull(params string[] strs)
{
foreach (var str in strs)
{
if (!strs.IsNullOrWhiteSpace(str))
return false;
}
return true;
}
如果您想要一个特定的数量null
字符串:
int CheckNullCount(params string[] strs)
{
int count;
foreach (var str in strs)
{
if (strs.IsNullOrWhiteSpace(str))
count++;
}
return count;
}
所有这些都可以这样称呼:
AreAllStringsFull(param1, param2, ...);
CheckAnyNulls(str1, str2, str3, str4, str5, str6, str7);
CheckAllNulls(str1, str2, str3, str4, str5, str6, str7);
int nullStrs = CheckNullCount(str1, str2, str3, str4, str5, str6, str7);
if(List.All(x=>string.IsNullOrWhiteSpace(x)))
{
}
或:
您可以将字符串传递到列表中,然后按如下方式进行检查:
AreAllStringsFull(param1, param2, ...);
CheckAnyNulls(str1, str2, str3, str4, str5, str6, str7);
CheckAllNulls(str1, str2, str3, str4, str5, str6, str7);
int nullStrs = CheckNullCount(str1, str2, str3, str4, str5, str6, str7);
if(List.All(x=>string.IsNullOrWhiteSpace(x)))
{
}
如果您考虑性能问题,那么,
str.Length==0
是最快的检查方法null@gypsyCoder这是检查空字符串的最快方法,而不是null
,因为如果它是null
,则会引发null引用异常,如果字符串为null
则会引发NullReferenceException
如果将所有字符串添加到一个字符串中,然后检查其长度是否短于7或是否为null?这仅在所有字符串均为null/空时有效。如果他想单独检查它们,以便它们都必须有东西,那么这将失败。(str1和str2=空,strN=某个值,然后检查通过)