C#面试问题 [英] C# interview question

查看:101
本文介绍了C#面试问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是接受记者采访时的问题,我需要帮助。

This is an interview question I need help with.

您有以下ASP.NET code-behind类:

You have the following ASP.NET code-behind class:

public partial class Page1 : Page 
{
    private string _value;

    public Page1() 
    {
        if (DateTime.Now.Ticks % 10 == 0)
            _value = "Test";
    }       

    ~Page1() 
    {
        if(_value.Equals("Test"))
            _value = string.Empty;      
    }
}

任何时候,有人请求该页面时,的w3wp.exe 进程意外终止。


  • 为什么会发生这种情况与用户看到死亡的黄色画面(默认的ASP.NET错误页)?

  • Why does this occur versus the user seeing a yellow screen of death (default ASP.NET error page)?

为什么会出现总是在托管堆中一个OutOfMemoryException present?

Why is there always an OutOfMemoryException present on the managed heap?

推荐答案

提示:从来​​不扔在析构函数/终结例外,否则将杀死其中GC运行,没有GC的东西可能会变得丑陋线程

Hint: never throw exceptions in a destructor/finalizer or you will kill the thread on which the GC runs and without GC things might get ugly.

虽然在.NET 1.1宽容一些朝着后台线程抛出异常而被消耗,下这不再CLR 2.0开始的情况下也不会带来宿主进程。 报价从DOC

While there was some tolerance in .NET 1.1 towards exceptions thrown in background threads which were consumed and wouldn't bring the hosting process down that's no longer the case starting from CLR 2.0. Quote from the doc:

如果敲定或敲定的重写
  抛出一个异常,并且运行时
  不是由一个应用程序的托管
  覆盖默认策略中,
  运行时终止该过程,并没有
  活动的try-finally块或
  终结执行。这种行为
  确保流程的完整性,如果
  终结不能免费或破坏
  资源。

If Finalize or an override of Finalize throws an exception, and the runtime is not hosted by an application that overrides the default policy, the runtime terminates the process and no active try-finally blocks or finalizers are executed. This behavior ensures process integrity if the finalizer cannot free or destroy resources.

在终结​​抛出异常是致命的。

Throwing an exception in a finalizer is fatal.

这篇关于C#面试问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆