简体   繁体   English

+带有空操作数的字符串连接运算符

[英]+ string concat operator with a null operand

A co-worker showed me a very strange behavior and I'd like to know if someone could explain me why. 一位同事向我展示了一种非常奇怪的行为,我想知道是否有人可以解释我原因。

A basic constructor with 2 string params: 一个有2个字符串参数的基本构造函数:

    public MyClass(string str1, string str2)
    {
        this.s1 = str1;
        this.s2 = str2;
        this.s3 = Method(str2 + "._className", str1);
    }

Method is: 方法是:

public string Method(string key, string defaultValue)
{
    List<string> list = _vars[key];
    if (list == null) return defaultValue;
    string res = "";
    foreach (string s in list)
    {
        if (res != "") res += ",";
        res += s;
    }
    return res;
}

When this ctor is called within an aspx page with str2 as null , all works fine because if an operand of string concatenation + is null , an empty string is substituted. 当在str2null的aspx页面内调用此ctor时,一切正常,因为如果字符串连接+的操作数为null ,则替换空字符串。

But when this ctor is called with str2 as null in a background thread, a NullReferenceException is fired. 但是当在后台线程中使用str2作为null调用此ctor时,会触发NullReferenceException

The problem was solved by testing str2 != null before using it, but I'd really like to know why the same code sometimes fires an exception, sometimes not! 在使用之前测试str2 != null解决了这个问题,但我真的很想知道为什么相同的代码有时会触发异常,有时候不会!

Here is the stack trace: 这是堆栈跟踪:

Exception: System.NullReferenceException 
Message: Object reference not set to an instance of an object.
StackTrace: 
at MyClass..ctor(String str1, String str2) 
at AbandonedCartsNotificationJob.NotifyAbandonedCarts() in AbandonedCartsNotificationJobPartial.cs:line 39 
at AbandonedCartsNotificationJob.work() in AbandonedCartsNotificationJob.cs:line 15 
at MyRuntime.JobManager.run() 
at System.Threading.ThreadHelper.ThreadStart_Context(Object state) 
at System.Threading.ExecutionContext.runTryCode(Object userData) 
at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData) 
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state) 
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) 
at System.Threading.ThreadHelper.ThreadStart()

There was an obscure bug in the .NET Framework's implementation of string concatenation, but it only affected concatenations of 4 objects, where one of the objects is non-null and provided an override of ToString that returned null. .NET Framework的字符串连接实现中存在一个模糊的错误,但它只影响4个对象的连接,其中一个对象是非空的, 提供了返回null的ToString的覆盖。 Clearly that situation isn't the case here. 显然,这种情况并非如此。

This situation is most likely caused by one of the following: 这种情况很可能是由以下原因之一引起的:

  • _vars is null when Method is called _vars Method_vars为null
  • Due to a misuse of _vars in a multi-threaded application, the internal state of _vars has been corrupted, resulting in a NullReferenceException when operator [] is used. 由于误用_vars在多线程应用程序,的内部状态_vars已被损坏,从而导致NullReferenceException当操作者[]被使用。

The problem lies in the implementation of the Method Object. 问题在于Method对象的实现。 Since the + Operator implementation interprets a null value as an empty string . 由于+ Operator实现将空值解释为空字符串 The actuall null value never enters the constructor when set in str2 . str2设置时,actuall null值永远不会进入构造函数。 On the Opposite, str1 directly enters as null value and may depending on the implementation cause a null reference exception. 在相反的情况下, str1直接作为空值输入,并且可能取决于实现导致空引用异常。

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

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