References and Objects in C #

I am currently writing an appointment for a school. I am having problems with how I should comment out different parts of my application.

I need to read from files, so I am using the StreamReader class.

When i write this

StreamReader reader;

      

I'm having trouble figuring out what I just did. C # Yellow Book by Rob Miles defines this very similar code

Account RobsAccount;

      

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

It follows by analogy with the luggage tag

You can think of them as something like a baggage tag as they can be tied to something with a rope. Once you have a tag, you can follow the rope to the object it is attached to.

When Rob Miles writes about this later

RobsAccount = new Account();

      

He describes it as

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

RobsAccount = new Account();

      

similar to my case where I just write

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

      

After reading several different books in C #, I still cannot comment with confidence on what is called. They seem to use different names and different counterparts.

So my question is:

What would be an appropriate comment on this

StreamReader reader;

      

And what would be an appropriate comment on this

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

      

My programming teacher says that when you write

StreamReader reader;

      

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

new StreamReader(file, Encoding.Default);

      

Is it correct to describe it?

+3


source to share


4 answers


StreamReader reader;

      

This is a variable declaration.



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

      

Here you create an instance of the "reader" variable with an object of the StreamReader class.

+3


source


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

StreamReader reader;

      

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



When you write the following line

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

      

You are instantiating an object reader

. That is, you are giving the value to the object reference that you created earlier. When you comment out this line, you can say that you have instantiated an object. After this point, you should not get a NullReference exception if you try to reference this object.

+1


source


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

If your task is to describe what is happening behind the scenes (for example, "Replay the computer"), then

StreamReader reader;

      

is an ad. This means that Java has reserved this name as a reference and knows that the reference will point to a location in memory that has data representing that type ("class").

When you do

new StreamReader(file, Encoding.Default);

      

In fact, you are modifying the contents of the container that the breakpoints belong to. The 'new' keyword tells Java that you want to get some memory, and the call to NewReader () changes that memory, while the JVM (virtual machine) maintains a name / reference-> container / data relationship.

0


source


When i write this

StreamReader reader; 

      

I'm having trouble figuring out what I just did. C # Yellow Book by Rob Miles defines this very similar code

Account RobsAccount;

      

  • The only thing that looks like is that in both cases a variable is declared.
  • Forget the analogy, although it is correct, it seems like you are more confusing than it does something good, if you like something, a hobby, etc. I can tailor it for you so you can relate to it ...

When Rob Miles writes about this later

RobsAccount = new Account(); 

      

He describes it as (...), creating an instance of the class and then attaching his tag to it.

So let's break this 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 an appropriate comment on this

StreamReader reader; 

      

And what would be an appropriate comment on 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: Each variable or constant is Type

    , it can be string

    , integer

    , a boolean

    , StreamReader

    or the type that you have defined using class

    or struct

    , any expression that evaluates the value is Type

    .
  • Class: A reference type

    refers to an object. Imagine your class register, it will refer to you, there are many registers, but they all refer to you as a person.
  • Struct: A value type

    that refers to a value. If your class registry specified your name by value and you had to change your name, the case would still refer to the old value.
  • Instantiation: create an instance of the class.
  • Object: This is what you get by instantiating the class
  • Variable: Is an instance Type

    , if a variable is value type

    , then it will always consist of a value, if a variable is reference type

    , then it can also be null, how it reference type

    might not refer to anything at all, however the value type will always have a default value. .. (don't be fooled by int?

    its value type! It just converts to Nullable<int>

    , but Nullable

    it actually isstruct

0


source







All Articles