简体   繁体   中英

References and objects in C#

I am currently writing an assignment for school. I have trouble on how I should comment different parts of my application.

I need to read from files, and therefore I use the StreamReader class.

When I write this

StreamReader reader;

I have trouble defining what I just did. The C# Yellow Book by Rob Miles defines this very similar code

Account RobsAccount;

What you actually get when the program obeys that line is the creation of a reference called RobsAccount.

He follows with an analogy to a luggage tag

You can think of them as a bit like a luggage tag, in that they can be tied to something with a piece of rope. If you have the tag you can then follow the rope to the object it is tied to.

When Rob Miles write this later on

RobsAccount = new Account();

He describes it as

(...)creating an instance of the class and then connecting our tag to it.

RobsAccount = new Account();

is similar to my case, where I simply write

reader = new StreamReader(file, Encoding.Default);

After reading a couple of different books on C#, I still cannot confidently comment what is called what. They seem to use different names and different analogies.

So my question is:

What would be a suitable comment to this

StreamReader reader;

And what would be a suitable comment to this

reader = new StreamReader(file, Encoding.Default);

My programming teacher says that when you write

StreamReader reader;

You are making a variable of the type StreamReader with the name reader . This variable is actually a pointer (hence, reference) to an object that is created by writing

new StreamReader(file, Encoding.Default);

Is this the right way to describe it?

StreamReader reader;

This is variable declaration.

reader = new StreamReader(file, Encoding.Default);

Here you are instantiating the variable "reader" with the object of StreamReader class.

There's a difference between declaring a variable and instantiating a variable in C#. When you write the line

StreamReader reader;

You are creating a reference to a null StreamReader object with the name reader. If you try to use it before instantiating it, you will get a null object reference. When you make your comment, you can state that you have declared an object reference.

When you write the following line

reader = new StreamReader(file, Encoding.Default);

You are instantiating the reader object. That is, you are giving a value to the object reference you created earlier. When you comment on this line, you can say that you have instantiated the object. After this point you should not receive a NullReference exception if you attempt to reference this object.

If you are talking about internal documentation, then you should comment on the functionality of variables when you declare them (what is it for--why did you make it). Here's a link for some quick tips on documentation: (download)

If your assignment is to describe what is happening behind the scenes (ie to "Play Computer") then

StreamReader reader;

is a declaration. It means that Java has reserved that name as a reference and it knows that reference will point to a location in memory that has data representing that type ('class').

When you do

new StreamReader(file, Encoding.Default);

You are actually modifying the contents of the container to which the reference points. The 'new' keyword tells Java you want to get some memory and the NewReader() function call modifies that memory while the JVM (virtual machine) maintains the name/reference->container/data relationship.

When I write this

 StreamReader reader; 

I have trouble defining what I just did. The C# Yellow Book by Rob Miles defines this very similar code

 Account RobsAccount; 
  • The only thing similar is that in both cases, a variable is being declared.
  • Forget the analogy, although it's correct, it seems to be confusing you more than it's doing any good, if there's something you enjoy doing, hobby etc I can tailor one for you so that you can relate to it...

When Rob Miles write this later on

 RobsAccount = new Account(); 

He describes it as (...)creating an instance of the class and then connecting our tag to it.

So let's break that down...

// The code below is instantiating a new object.
// new Account();

// The code below is declaring a variable, at the moment it is null
// This is because it isn't referencing any objects that we've created.
Account RobsAccount;

// The `=` character is an assignment operator, and we'll need it to 
// assign objects that we create to variables.
// The code below will assign an object of type Account, to the variable
// RobsAccount which has a type of Account.
RobsAccount = new Account();

// Now the `RobsAccount` variable is pointing to the object
// that we just instantiated

Moving on to your final question...

What would be a suitable comment to this

 StreamReader reader; 

And what would be a suitable comment to this

 reader = new StreamReader(file, Encoding.Default); 
// A declaration of a StreamReader variable.
StreamReader reader;

// Assigning a the `reader` variable with a new StreamReader object 
// that has been instantiated with the following parameters
// - The file path of the document to read
// - The encoding of the file to read
reader = new StreamReader(file, Encoding.Default);

Keywords:

  • Type: Every variable or constant has a Type , it might be a string , an integer , a boolean , StreamReader or a type that you've defined using a class or struct , any expression that evaluates to a value has a Type .
  • Class: A reference type references an object. Imagine your class register, it will reference you, there are many registers, but they all reference you as a person.
  • Struct: A value type that references a value. If your class register was to reference your name by value, and you were to change your name, the register would still be referencing the old value.
  • Instantiating: To create an instance of a class.
  • Object: Is what you receive as a result of instantiating a class
  • Variable: Is an instance of a type , if the variable is a value type , then it will always consist of a value, if the variable is a reference type , then it may also be null, as a reference type may not be referencing anything at all, however, a value type will always have default value... (Don't be tricked by int? it's a value type! It just translates into Nullable<int> , but the Nullable type is in fact a struct

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM