我爱string.IsNullOrEmpty
方法.我很想拥有能够为IEnumerable提供相同功能的东西.有这样的吗?也许一些收集助手班?我问的原因是,在if
语句中,如果模式是代码,代码看起来很混乱(mylist != null && mylist.Any())
.拥有它会更加清洁Foo.IsAny(myList)
.
这篇文章没有给出答案:IEnumerable是空的?.
我需要在JS中编写一些扩展方法.我知道如何在C#中做到这一点.例:
public static string SayHi(this Object name)
{
return "Hi " + name + "!";
}
Run Code Online (Sandbox Code Playgroud)
然后叫:
string firstName = "Bob";
string hi = firstName.SayHi();
Run Code Online (Sandbox Code Playgroud)
我如何在JavaScript中执行此类操作?
在null实例(扩展方法不允许)的情况下调用扩展方法时,您认为是最好的异常类型是什么?由于扩展方法只不过是静态方法,你可以说它应该是ArgumentNullException,但另一方面它们像实例方法一样使用,因此使用NullReferenceException可能更自然.我们来看下面的例子:
public static string ToInvariantString(this IFormattable value, string format)
{
return value.ToString(format, CultureInfo.InvariantCulture);
}
Run Code Online (Sandbox Code Playgroud)
这样,如果value参数为null,则抛出NullReferenceException.
另一个例子是:
public static string ToInvariantString(this IFormattable value, string format)
{
if (value == null) throw new ArgumentNullException("value");
return value.ToString(format, CultureInfo.InvariantCulture);
}
Run Code Online (Sandbox Code Playgroud)
编辑: 在一些答案中,你已经指出扩展方法可以像静态方法一样调用,在这种情况下,空引用异常会出错,这是一个很好的观点,实际上是我关注的一个问题,不知道为什么我忘了首先在问题中提到这一点.
有人还指出抛出NullReferenceException是错误的,是的,确实如此.这就是为什么我不扔它,我只是让它发生(让CLR抛出它)不守护方法.
我认为我赞成ArgumentNullException(这是我到目前为止所使用的)但我仍然认为至少有空间来反对NullReferenceException,因为在大多数将要使用该方法的地方看起来更自然.
.
始终调用带有运算符的扩展方法,即使对象为空而不抛出NullReferenceException
.通过使用运算符,?.
它永远不会调用.例如:
using System;
public class Program
{
public static void Main()
{
A a = null;
a.b(); // works
a?.b(); // doesn't work
}
}
public class A { }
public static class ext
{
public static void b(this A a)
{
Console.WriteLine("I'm called");
}
}
Run Code Online (Sandbox Code Playgroud)
为什么在这种情况下不调用扩展方法?这是一个ambiguos功能?
public static IFoo Bar<T>(this IFoo target, ...)
{
// I'm curious about how useful this check is.
if (target == null) throw new ArgumentNullException("target");
...
}
Run Code Online (Sandbox Code Playgroud)
(1)上面的代码对我来说似乎很奇怪,因为我觉得在任何情况下调用代码都是检查它的人.这个领域的扩展方法是否有些微妙适用?
(2)是否存在利用目标可以为空的事实的合法模式?我想这是因为想知道为什么在空引用上调用扩展方法不会像在null引用上调用实例方法那样生成运行时异常.
是否可以在Dictionary上创建一个扩展方法,如果Dictionary为null,则能够自我初始化?
我正在考虑以下几点:
public static void AddOrUpdate<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, TValue value)
{
// Initialize the Dictionary if null
dictionary = dictionary ?? new Dictionary<TKey, TValue>();
// Update or insert value
dictionary[key] = value;
}
Run Code Online (Sandbox Code Playgroud)
实际使用示例:
// null Dictionary
Dictionary<string, string> dict = null;
// Self-initializing
dict.AddOrUpdate("Key", "Value");
Run Code Online (Sandbox Code Playgroud)
或作为财产:
// Self-initializing
class.nullDict.AddOrUpdate("Key", "Value");
Run Code Online (Sandbox Code Playgroud) 我可以定义一个扩展方法来确定一个对象是否为null
public static bool IsNull(this object obj) {
if (obj == null)
return true;
else return false;
}
Run Code Online (Sandbox Code Playgroud)
但我也可以这样做:
public static bool IsNull<T>(this T obj) {
if(obj == null)
return true;
else return false;
}
Run Code Online (Sandbox Code Playgroud)
两者都适用于每个对象.这个T的目的是什么?进一步阐述预期的类型?如果是的话,为什么这样:typeof(T)
是可能的?那么(正如@MatthewWatson指出的那样,这无论如何都不起作用)(this T obj) where T: int)
(在哪里)背后的原因是什么?
这么多的问题.
我有一个扩展方法ParseItemsToIntegers
,它是一个字符串数组的扩展方法,我正在使用它,就像使用任何扩展一样.
var ids = ChkBoxSelected.ParseItemsToIntegers();
Run Code Online (Sandbox Code Playgroud)
我观察到的行为是,如果ChkBoxSelected
为null,它将毫无问题地调用扩展方法,但是在扩展方法中它会在空字符串数组上抛出空引用异常.
它是如何解决null上的扩展方法的?
为什么ChkBoxSelected
在调用扩展方法时它没有抛出空引用异常?
c# ×6
.net ×4
c#-6.0 ×2
linq ×2
collections ×1
dictionary ×1
exception ×1
ienumerable ×1
javascript ×1