TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Tech MC
NA
1
0
Objects variables fields vs Objects in functions
Jun 21 2010 9:03 PM
Hello All,
I am new to the development world and have a question. In the following code below, "FileStream fileStream" and "StreamWriter streamWriter;" are both object variables, correct? (if not, what do you call them?)
Why would you declare it as a field as oppose to instatiating the class like "StreamWriter streamWriter = new StreamWriter();" and using it in your functions?
I've seen this before in other places used many times.
What advantages does that give you?
Sample Code
public class FileLogger
{
//what do you call this and why use it this way?
FileStream fileStream;
StreamWriter streamWriter;
public FileLogger(string filename)
{
fileStream = new FileStream(filename, FileMode.Create);
streamWriter = new StreamWriter(fileStream);
}
public void Logger(string s)
{
streamWriter.WriteLine(s);
}
}
Reply
Answers (
1
)
Please tell me the difference between .net 3.0 and 3.5 and 4.0
Calendar should default to earlier phase end date - not current date