473,395 Members | 1,766 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,395 software developers and data experts.

When to use null

Hi,

Here is a small example function. What I would like to know is when I
should be using null:

private bool MyTestFunc(string Message)
{
StreamWriter sw=null;
string str=null;

sw = new StreamWriter(FILE_PATH,true);

//Do some stuff with the SW
str = String.Format(@"{0}|{1}|",CLASS_NAME,message);
sw.WriteLine(str);
sw.Close();

//Not sure what to do here, but here is my guess
sw=null;
str=null;
//If I didnt set these to null, would that mean the
//The GC wouldnt clean these objects up?

return true;
}
Any help on this would be appreciated.

Regards,

Steven
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #1
3 1750
Steven Blair <st**********@btinternet.com> wrote:
Here is a small example function. What I would like to know is when I
should be using null:


See http://blogs.msdn.com/csharpfaq/arch.../26/97229.aspx

What you *should* do is close your writer in a more reliable manner.
I'd also recommend only declaring variables at the point of first use.
I'd have written your method as:

void MyTestFunc (string message)
{
using (StreamWriter sw = new StreamWriter(FilePath, true))
{
sw.WriteLine ("{0}|{1}|", ClassName, message);
}
}

Differences:

o Constants follow MS naming conventions
o Parameter name follows MS naming conventions
o No need for the temporary string variable - the version of
WriteLine I use does the formatting anyway
o The StreamWriter is closed by the automatic call to Dispose
o String literal is now normal, rather than verbatim (as it
doesn't need to be verbatim)
o No declaration before use
o No unnecessary assignment to null
o No return value - on error, an exception will be thrown

Note that it still isn't thread-safe. Whether or not that's important
to you, I don't know.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2
Ok, point taking. I will change my code to match what you have demontsrated.
But one of the points I was trying to get across was the use of strings.

Another small example:

void MyFunc()
{
string tmp=null;

tmp="Not very useful, only for testing";

//Do something with tmp

tmp=null // Is this neccessary?
}

Regards,

Steven

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Steven Blair <st**********@btinternet.com> wrote:
Here is a small example function. What I would like to know is when I
should be using null:


See http://blogs.msdn.com/csharpfaq/arch.../26/97229.aspx

What you *should* do is close your writer in a more reliable manner.
I'd also recommend only declaring variables at the point of first use.
I'd have written your method as:

void MyTestFunc (string message)
{
using (StreamWriter sw = new StreamWriter(FilePath, true))
{
sw.WriteLine ("{0}|{1}|", ClassName, message);
}
}

Differences:

o Constants follow MS naming conventions
o Parameter name follows MS naming conventions
o No need for the temporary string variable - the version of
WriteLine I use does the formatting anyway
o The StreamWriter is closed by the automatic call to Dispose
o String literal is now normal, rather than verbatim (as it
doesn't need to be verbatim)
o No declaration before use
o No unnecessary assignment to null
o No return value - on error, an exception will be thrown

Note that it still isn't thread-safe. Whether or not that's important
to you, I don't know.

--
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
Sorry, last post is not needed now. Read the articel you supplied a link for
and that answers my secodn question :$

Regards,

Steven

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Steven Blair <st**********@btinternet.com> wrote:
Here is a small example function. What I would like to know is when I
should be using null:


See http://blogs.msdn.com/csharpfaq/arch.../26/97229.aspx

What you *should* do is close your writer in a more reliable manner.
I'd also recommend only declaring variables at the point of first use.
I'd have written your method as:

void MyTestFunc (string message)
{
using (StreamWriter sw = new StreamWriter(FilePath, true))
{
sw.WriteLine ("{0}|{1}|", ClassName, message);
}
}

Differences:

o Constants follow MS naming conventions
o Parameter name follows MS naming conventions
o No need for the temporary string variable - the version of
WriteLine I use does the formatting anyway
o The StreamWriter is closed by the automatic call to Dispose
o String literal is now normal, rather than verbatim (as it
doesn't need to be verbatim)
o No declaration before use
o No unnecessary assignment to null
o No return value - on error, an exception will be thrown

Note that it still isn't thread-safe. Whether or not that's important
to you, I don't know.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #4

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

Similar topics

0
by: Soefara | last post by:
Dear Sirs, I am experiencing strange results when trying to optimize a LEFT JOIN on 3 tables using MySQL. Given 3 tables A, B, C such as the following: create table A ( uniqueId int not...
0
by: Jerry | last post by:
Below is ALL the code for all the databases... Here's the problem: I callup the aspx file in IE and the form comes up just fine. When I select a person to update, I get the subject error. ...
10
by: Lyle Fairfield | last post by:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbaac11/html/acfctNZ_HV05186465.asp "If the value of the variant argument is Null, the Nz function returns the number zero or a...
17
by: Mark A | last post by:
DB2 8.2 for Linux, FP 10 (also performs the same on DB2 8.2 for Windoes, FP 11). Using the SAMPLE database, tables EMP and EMLOYEE. In the followng stored procedure, 2 NULL columns (COMM) are...
3
by: Ricardo Vazquez | last post by:
Hi! I have a new problem with this SCENARIO I already described in a previous post: - PBX (a private telephone exchange or switch) - A Telephony Server Application running on computer "A" (it...
11
by: MikeT | last post by:
This may sound very elementary, but can you trap when your object is set to null within the object? I have created a class that registers an event from an object passed in the constructor. When...
1
by: barrathi | last post by:
Hi all i need one query help! for reporting purpose i wrote one query - follows select s.tran_empid as EmpId, e.M_EMPL_NAME as EmpName, p.PROJ_NAME as Project, t.TITL_NAME as Title,CASE WHEN...
6
by: =?Utf-8?B?U2hhd24gU2VzbmE=?= | last post by:
Greetings! I was researching AJAX to provide a solution to displaying status messages while a long process executed. I found several examples online and was able to use their code to get a quick...
9
by: Francois Grieu | last post by:
When running the following code under MinGW, I get realloc(p,0) returned NULL Is that a non-conformance? TIA, Francois Grieu #include <stdio.h> #include <stdlib.h>
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.