gpt4 book ai didi

c# - String.IsNullOrEmpty() 或 IsEmpty()

转载 作者:太空狗 更新时间:2023-10-29 17:37:20 24 4
gpt4 key购买 nike

我刚刚注意到在字符串上他们有很多扩展方法,我想我从来没有在字符串上注意到过。

有些像

IsEmpty() // Seems to be equivalent to  String.IsNullOrEmpty() 
AsInt() // seems to be equivalent to Convert.ToInt32(string); - does it throw exception as well?

我只是想知道他们是否在 hook 下使用了相同的代码,这些代码只是为了减少打字或更多的事情要做?

虽然像这样,但有些似乎确实丢失了

 String.IsNullOrWhiteSpace()

编辑

抱歉,当我说缺少 String.IsNullOrWhiteSpace() 时,我提到没有扩展方法。我确实有那个方法,就是我在上面写了我所做的。

这些似乎也不是框架中的标准,所以我想弄清楚它们是从哪里来的?

我不确定 resharper 是否添加了这些或者我是否有其他引用资料。我不认为我曾经导入过任何扩展插件。

当我点击 IsEmpty() 上的定义时

我明白了

#region Assembly System.Web.WebPages.dll, v4.0.30319
// c:\Program Files (x86)\Microsoft ASP.NET\ASP.NET Web Pages\v1.0\Assemblies\System.Web.WebPages.dll
#endregion

using System;
using System.Runtime.CompilerServices;

namespace System.Web.WebPages
{
// Summary:
// Provides utility methods for converting string values to other data types.
public static class StringExtensions
{
// Summary:
// Converts a string to a strongly typed value of the specified data type.
//
// Parameters:
// value:
// The value to convert.
//
// Type parameters:
// TValue:
// The data type to convert to.
//
// Returns:
// The converted value.
public static TValue As<TValue>(this string value);
//
// Summary:
// Converts a string to the specified data type and specifies a default value.
//
// Parameters:
// value:
// The value to convert.
//
// defaultValue:
// The value to return if value is null.
//
// Type parameters:
// TValue:
// The data type to convert to.
//
// Returns:
// The converted value.
public static TValue As<TValue>(this string value, TValue defaultValue);
//
// Summary:
// Converts a string to a Boolean (true/false) value.
//
// Parameters:
// value:
// The value to convert.
//
// Returns:
// The converted value.
public static bool AsBool(this string value);
//
// Summary:
// Converts a string to a Boolean (true/false) value and specifies a default
// value.
//
// Parameters:
// value:
// The value to convert.
//
// defaultValue:
// The value to return if value is null or an invalid value. The default is
// false.
//
// Returns:
// The converted value.
public static bool AsBool(this string value, bool defaultValue);
//
// Summary:
// Converts a string to a System.DateTime value.
//
// Parameters:
// value:
// The value to convert.
//
// Returns:
// The converted value.
public static DateTime AsDateTime(this string value);
//
// Summary:
// Converts a string to a System.DateTime value and specifies a default value.
//
// Parameters:
// value:
// The value to convert.
//
// defaultValue:
// The value to return if value is null or an invalid value. The default is
// the minimum time value on the system.
//
// Returns:
// The converted value.
public static DateTime AsDateTime(this string value, DateTime defaultValue);
//
// Summary:
// Converts a string to a System.Decimal number.
//
// Parameters:
// value:
// The value to convert.
//
// Returns:
// The converted value.
public static decimal AsDecimal(this string value);
//
// Summary:
// Converts a string to a System.Decimal number and specifies a default value.
//
// Parameters:
// value:
// The value to convert.
//
// defaultValue:
// The value to return if value is null or invalid.
//
// Returns:
// The converted value.
public static decimal AsDecimal(this string value, decimal defaultValue);
//
// Summary:
// Converts a string to a System.Single number.
//
// Parameters:
// value:
// The value to convert.
//
// Returns:
// The converted value.
public static float AsFloat(this string value);
//
// Summary:
// Converts a string to a System.Single number and specifies a default value.
//
// Parameters:
// value:
// The value to convert.
//
// defaultValue:
// The value to return if value is null.
//
// Returns:
// The converted value.
public static float AsFloat(this string value, float defaultValue);
//
// Summary:
// Converts a string to an integer.
//
// Parameters:
// value:
// The value to convert.
//
// Returns:
// The converted value.
public static int AsInt(this string value);
//
// Summary:
// Converts a string to an integer and specifies a default value.
//
// Parameters:
// value:
// The value to convert.
//
// defaultValue:
// The value to return if value is null or is an invalid value.
//
// Returns:
// The converted value.
public static int AsInt(this string value, int defaultValue);
//
// Summary:
// Checks whether a string can be converted to the specified data type.
//
// Parameters:
// value:
// The value to test.
//
// Type parameters:
// TValue:
// The data type to convert to.
//
// Returns:
// true if value can be converted to the specified type; otherwise, false.
public static bool Is<TValue>(this string value);
//
// Summary:
// Checks whether a string can be converted to the Boolean (true/false) type.
//
// Parameters:
// value:
// The string value to test.
//
// Returns:
// true if value can be converted to the specified type; otherwise, false.
public static bool IsBool(this string value);
//
// Summary:
// Checks whether a string can be converted to the System.DateTime type.
//
// Parameters:
// value:
// The string value to test.
//
// Returns:
// true if value can be converted to the specified type; otherwise, false.
public static bool IsDateTime(this string value);
//
// Summary:
// Checks whether a string can be converted to the System.Decimal type.
//
// Parameters:
// value:
// The string value to test.
//
// Returns:
// true if value can be converted to the specified type; otherwise, false.
public static bool IsDecimal(this string value);
//
// Summary:
// Checks whether a string value is null or empty.
//
// Parameters:
// value:
// The string value to test.
//
// Returns:
// true if value is null or is a zero-length string (""); otherwise, false.
public static bool IsEmpty(this string value);
//
// Summary:
// Checks whether a string can be converted to the System.Single type.
//
// Parameters:
// value:
// The string value to test.
//
// Returns:
// true if value can be converted to the specified type; otherwise, false.
public static bool IsFloat(this string value);
//
// Summary:
// Checks whether a string can be converted to an integer.
//
// Parameters:
// value:
// The string value to test.
//
// Returns:
// true if value can be converted to the specified type; otherwise, false.
public static bool IsInt(this string value);
}
}

最佳答案

这些不是“标准”扩展方法 - 很可能是其他人在您的项目中添加的。这意味着我们无法判断代码在幕后做了什么,但您应该能够自己找出答案。

在 Visual Studio 中,您应该能够导航到任一方法的定义 - 它会显示该方法所在的程序集,或者如果可以的话直接转到源代码。

编辑:鉴于评论,它们看起来像是来自 MVC's StringExtensions class 的扩展方法...据我所知,这违反了命名中的各种不良做法 - 特别是在方法名称中使用语言特定名称而不是 CLR 类型名称。 (例如,AsFloat 应该是 AsSingle。)我还认为它应该是“To”而不是“As”,因为它提供的是完全转换而不是只是返回原始值的 View 。所有的骗局。

关于c# - String.IsNullOrEmpty() 或 IsEmpty(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4995350/

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