Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/257.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
简单的类实例化在C#中会失败吗?_C#_.net_Asp.net_Instantiation_Out Of Memory - Fatal编程技术网

简单的类实例化在C#中会失败吗?

简单的类实例化在C#中会失败吗?,c#,.net,asp.net,instantiation,out-of-memory,C#,.net,Asp.net,Instantiation,Out Of Memory,我看到另一个开发人员编写的一些代码如下: var stringBuilder = new StringBuilder(); if(stringBuilder == null) { // Log memory allocation error // ... return; } (代码中到处都是) 问题1: 那个错误记录代码会被调用吗?如果没有内存,第一行不会抛出System.OutOfMemoryException 问题2: 调用构造函数会返回null吗?您是正确的,而

我看到另一个开发人员编写的一些代码如下:

var stringBuilder = new StringBuilder();

if(stringBuilder == null)
{
    // Log memory allocation error
    // ...
    return;
}
(代码中到处都是)

问题1: 那个错误记录代码会被调用吗?如果没有内存,第一行不会抛出
System.OutOfMemoryException

问题2:
调用构造函数会返回null吗?

您是正确的,而代码是错误的。失败时,它将抛出
OutOfMemoryException
。这一点在以下方面是明确的:

“如果新操作员未能分配 内存,它抛出异常 OutOfMemoryException。“


构造函数不返回任何内容,更不用说null了。他们操纵一个已经分配的对象。

< P>我的假设是编码器工作在C++中,不知道C中的事情是如何工作的。
  • 否。如果没有足够的内存分配对象,将引发OutOfMemoryException
  • 没有

  • 现在,这个代码是另一个故事:

    StringBuilder stringBuilder = null;
    
    try { stringBuilder = new StringBuilder(); } catch(Exception) {}
    
    if(stringBuilder == null)
    {
        // Log memory allocation error
        // ...
        return;
    }
    

    在这种情况下,字符串生成器可能(可以想象)为空。

    下面是一个更好的代码版本。但是,如果没有足够的内存来分配引用,您将遇到更大的问题

    StringBuilder stringBuilder = null;
    
    try {
       stringBuilder = new StringBuilder();
    }
    catch(OutOfMemoryException) {
       // log memory error
    }
    

    对于C++来说,代码同样是错误的。C++ <代码>新< /COD>当分配失败时,抛出<代码> BADYOLLC/<代码>,除非您明确地告诉它不要这样做。这正是实习生所说的。我自己没有接触过很多C++,而且不是很长时间。“谢谢。”马修:是的,如果你使用的是标准C++(并且知道你在做什么)。前标准C++返回空,显然作者没有保持技能。实际上,StrugBu建器不会是代码> null <代码>,它不会被初始化。实际上,它甚至不会编译。但我认为我们都可以假设Brian打算编写
    StringBuilder StringBuilder=null取而代之:)对不起,伙计们。修复了示例中的代码。谁需要编译器?我应该编写一个服务,将我的代码发送给其他人进行评估:)正如Marc Gravell指出的,从技术上讲,代理类和可空值是可能的,但这是一个病态的案例,不值得考虑: