473,385 Members | 1,341 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

static variables in C#

Hi,

I have a quick question. I am using a static variable to hold an instance
of the StreamWriter:
private static StreamWriter sw = File.AppendText("C:\\Temp\\Test.txt");

I am curious to find out how static resources are being disposed in C#. The
problem that I am having is I am opening to a file, write a text message and
then close it. However, if I try to write to the same file again, I
generate an ObjectDisposed exception, since the file was closed and the
FileStream has been disposed. So, I was thinking to let the GC to release
allocated resources and not call on Close method explicitly. But since the
StreamWriter resource that I am using is static, would the GC dispose it?
What is the lifecycle of static variables? What part of the memory do
static variables/methods reside in?

Thanks,
Natalia

Nov 16 '05 #1
3 3265
Hi Natalia,

The static variable is created when the program starts and is destroyed
(collected by GC) when the program ends.
Happy coding!
Morten Wennevik [C# MVP]
Nov 16 '05 #2
Natalia DeBow <na***********@unisys.com> wrote:
I have a quick question. I am using a static variable to hold an instance
of the StreamWriter:
private static StreamWriter sw = File.AppendText("C:\\Temp\\Test.txt");

I am curious to find out how static resources are being disposed in C#. The
problem that I am having is I am opening to a file, write a text message and
then close it. However, if I try to write to the same file again, I
generate an ObjectDisposed exception, since the file was closed and the
FileStream has been disposed. So, I was thinking to let the GC to release
allocated resources and not call on Close method explicitly. But since the
StreamWriter resource that I am using is static, would the GC dispose it?
What is the lifecycle of static variables? What part of the memory do
static variables/methods reside in?


The StreamWriter itself isn't static - only the variable is static.
Objects themselves have no notion of being static, public, private,
etc.

Now, your variable will prevent the StreamWriter from being collected
until the AppDomain is unloaded, which is usually at program
termination.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #3
Thanks so much for your response! I does make a lot more sense now.

Natalia

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: James | last post by:
Hello Java NG, I not sure if this is the right NG for this type of question but if not please let me know which is, TIA Any way first off let me say I'm a student and this WAS last weeks lab,...
2
by: katekukku | last post by:
HI, Could anyone please tell me what are static variables and what exactly are there features. I am a little bit confused. Thank You
9
by: Bryan Parkoff | last post by:
I have noticed that C programmers put static keyword beside global variable and global functions in C source codes. I believe that it is not necessary and it is not the practice in C++. Static...
4
by: Dave | last post by:
I used the following class and .aspx code below to understand how static works on variables and methods taken from...
8
by: Simone Chiaretta | last post by:
I've a very strange behaveour related to a website we built: from times to times, something should happen on the server, and all static variables inside the web application, both defined inside aspx...
28
by: Dennis | last post by:
I have a function which is called from a loop many times. In that function, I use three variables as counters and for other purposes. I can either use DIM for declaring the variables or Static. ...
5
by: Jesper Schmidt | last post by:
When does CLR performs initialization of static variables in a class library? (1) when the class library is loaded (2) when a static variable is first referenced (3) when... It seems that...
9
by: CDMAPoster | last post by:
About a year ago there was a thread about the use of global variables in A97: http://groups.google.com/group/comp.databases.ms-access/browse_frm/thread/fedc837a5aeb6157 Best Practices by Kang...
55
by: Zytan | last post by:
I see that static is more restricted in C# than in C++. It appears usable only on classes and methods, and data members, but cannot be created within a method itself. Surely this is possible in...
16
by: RB | last post by:
Hi clever people :-) I've noticed a lot of people stating not to use static variables with ASP.NET, and, as I understand it, the reason is because the variable is shared across user sessions -...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.