为什么不处置 [英] Why isn't it disposing

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

问题描述

全部:

All:

private void AppendData(string strData, string strFileName)
        {
            FileStream fStream;
            StreamWriter sWriter;
            try
            {
                fStream = new FileStream(strFileName, FileMode.Append);
                sWriter = new StreamWriter(fStream);
                sWriter.WriteLine(strData);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                sWriter.Close();
                sWriter.Dispose();
                fStream.Close();
                fStream.Dispose();
            }
        }


上面的代码在编译过程中告知未分配的局部变量的使用.它指向finally块内的sWriter和fStream".我这样做的原因是,我在一个论坛上读到了.NET不需要我们显式初始化变量.我的理解错了吗?

如果我将其初始化为NULL,则代码可以正常工作.


The above code tells during compiling that "Use of unassigned local variable. It points to sWriter and fStream inside the finally block". Why I did it this way was, I read in a forum that .NET does not require us to explicitly initialize a variable. Is my understanding wrong ?

The code works fine if I initialize it to NULL.

private void AppendData1(string strData, string strFileName)
        {
            FileStream fStream = null;
            StreamWriter sWriter = null;
            try
            {
                fStream = new FileStream(strFileName, FileMode.Append);
                sWriter = new StreamWriter(fStream);
                sWriter.WriteLine(strData);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                sWriter.Close();
                sWriter.Dispose();
                fStream.Close();
                fStream.Dispose();
            }
        }


您能在这里提供一些启示吗?


Can you throw some light here...

推荐答案

如果编译器可以看到通过软件的任何路由,使得变量没有值,则会抱怨.存在这样的路线:
If the compiler can see any route through the software such that the variable does not get a value, it complains. Such a route exists:
FileStream fStream;
StreamWriter sWriter;
try
   {
   fStream = new FileStream(strFileName, FileMode.Append);

此行导致异常-fStream未被修改.
这些行被跳过:

This line causes an exception - fStream is not modified.
These lines are skipped:

sWriter = new StreamWriter(fStream);
sWriter.WriteLine(strData);
}

执行在这里继续:

Execution continues here:

catch (Exception ex)
   {
   MessageBox.Show(ex.Message);
   }

然后执行finally块:

Then the finally block is executed:

finally
   {


自声明以来,这是对sWriter的第一个引用.


This is the first reference to sWriter since it was declared.

sWriter.Close();
sWriter.Dispose();

这是自声明以来对fStream的第一个引用.

This is the first reference to fStream since it was declared.

fStream.Close();
fStream.Dispose();
}



[edit]
将其设置为null可以避免编译问题,但会引入另一个问题:如果触发了try/catch,则finally块将导致异常.
在尝试关闭或处置之前,您需要检查每个变量是否为null.我建议将其重写为:



[edit]
Setting it to null gets rid of the compilation problem, but introduces another: your finally block will cause an exception if your try/catch is triggered.
You need to check for null in each before you try to Close or Dispose. I would suggest rewriting this as:

private void AppendData(string strData, string strFileName)
        {
            try
            {
                using (FileStream fStream = new FileStream(strFileName, FileMode.Append))
                {
                     using (StreamWriter sWriter = new StreamWriter(fStream))
                     {
                          sWriter.WriteLine(strData);
                     }
                 }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }


-OriginalGriff [/edit]


- OriginalGriff[/edit]




虽然由OriginalGiff编辑的代码是完美的.使用using 是处置任何变量的最佳方法.但是如果您需要在开始时初始化变量,则不要分配空值,而应使用该类型的默认值.

例如,如果我们有自定义类Customer,则我们已经在类中实现了IDisposable,如果您要使用finally块,那么好的做法是使用


Hi,

Although the Edited code by OriginalGiff is perfect. use of using is the best way to dispose any of the variable. but in case you need to initialize your variable in the beginning then don''t assign null, instead use the default value of that type.

say for example, if we have our custom class Customer , we have implemented IDisposable with our class and if you want to use finally block then good practice is to use like,

good
Customer newCustomer = default(Customer); 


不好


bad

Customer newCustomer = null;



原因是,如果您的类更改为不可为空,那么您需要在所有地方进行更改.相反,以上将对您将来有所帮助.

就Dispose而言,请遵循OriginalGriff的方法.

谢谢
-Amit



Reason is if your class is changed to not Nullable then you need to change everywhere. instead above will be helpful to you in future.

And as far as Disposing is concern follow the way of OriginalGriff.

Thanks
-Amit


这篇关于为什么不处置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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